News

`hasMany()` Collection Method in Laravel 12.50.0

Published
`hasMany()` Collection Method in Laravel 12.50.0 image

Release updates

Never miss a Laravel release

Get an email whenever a new Laravel release lands.

Release Date: February 4, 2026
Laravel Version: 12.50.0

Summary

Laravel v12.50.0 introduces a new hasMany() collection method for checking if a collection contains multiple matching items, along with support for unique queued listeners, typed cache getters, and a new model method for excluding appended attributes. This release continues Laravel's focus on developer experience improvements with expanded enum support and numerous bug fixes.

Key highlights include:

  • New hasMany() collection method
  • Unique queued listeners support
  • withoutAppends() method for models
  • Typed getters for Cache facade
  • authority() method for URI parsing
  • Enum support for cache array keys
  • Numerous bug fixes and type improvements

What's New

hasMany() Collection Method

Laravel 12.50.0 adds the hasMany() method to collections, providing a straightforward way to check if a collection contains multiple items matching specified criteria. This method serves as the inverse of hasSole() (introduced in v12.49.0) - where hasSole() confirms exactly one match, hasMany() confirms multiple matches.

// Check if collection has multiple items
$collection->hasMany();

// With a callback filter
$users->hasMany(fn ($user) => $user->isActive());

// With key/value pair
$orders->hasMany('status', 'pending');

// With operator syntax
$products->hasMany('price', '>=', 100);

This method is particularly useful for validation logic, conditional workflows, and filtering scenarios where you need to confirm that multiple items exist before proceeding with an operation.

Pull Request: #58550

Unique Queued Listeners

Queued event listeners can now implement the ShouldBeUnique and ShouldBeUniqueUntilProcessing contracts, preventing duplicate listeners from being dispatched to the queue in rapid succession:

namespace App\Listeners;

use App\Events\LicenseSaved;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;

class AcquireProductKey implements ShouldQueue, ShouldBeUnique
{
    public function uniqueId(LicenseSaved $event): string
    {
        return $event->license->id;
    }

    public function __invoke(LicenseSaved $event): void
    {
        // Process event...
    }
}

This feature mirrors the unique job pattern and solves a common production issue where listeners are dispatched to the queue multiple times when they should only run once per unique identifier. Unlike the WithoutOverlapping middleware which only prevents concurrent execution, ShouldBeUnique prevents duplicate jobs from entering the queue initially.

Pull Request: #58402

withoutAppends() Model Method

A new withoutAppends() method allows you to selectively remove appended attributes from model instances, improving performance for API responses and data serialization:

// Remove all appended attributes
$user->withoutAppends();

// Remove specific appended attributes
$user->withoutAppends(['full_name', 'avatar_url']);

This is particularly useful when you have models with computationally expensive appended attributes that aren't needed for every use case. Instead of defining multiple separate model classes or resource transformations, you can now conditionally exclude appends on a per-request basis.

Pull Request: #58552

Typed Cache Getters

The Cache facade now includes typed getter methods that provide type safety and better IDE support when retrieving cached values:

// Get integer with default
$count = Cache::integer('view_count', 0);

// Get string with default
$name = Cache::string('user_name', 'Guest');

// Get boolean with default
$isActive = Cache::boolean('feature_enabled', false);

// Get float with default
$price = Cache::float('product_price', 0.0);

// Get array with default
$items = Cache::array('cart_items', []);

These methods ensure type consistency and make it clearer what type of value is expected from the cache, reducing runtime type errors and improving code readability.

Pull Request: #58451

authority() URI Method

The Support\Uri class now includes an authority() method for extracting the authority component from a URI:

$uri = Uri::of('https://user:pass@example.com:8080/path');
$authority = $uri->authority(); // "user:pass@example.com:8080"

The authority component consists of the user information, host, and port of a URI, useful when parsing or validating URLs.

Pull Request: #58534

Enum Support for Cache Array Keys

Continuing Laravel's expanded enum support, Cache::get() now accepts enum values when using array keys:

$values = Cache::get([CacheKey::User, CacheKey::Settings]);

This builds on previous enum support additions in session and cache methods, providing type-safe cache key management throughout your application.

Pull Request: #58616

References

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Laravel AutoBuilder: Build Visual Automation Workflows image

Laravel AutoBuilder: Build Visual Automation Workflows

Read article
Pun: Format, Lint, and Analyze PHP With One Rust Binary image

Pun: Format, Lint, and Analyze PHP With One Rust Binary

Read article
Form Request Model Lookups and PHP 8.6 Support in Laravel 13.44 image

Form Request Model Lookups and PHP 8.6 Support in Laravel 13.44

Read article
Generate Laravel Validation Rules From a JSON Schema image

Generate Laravel Validation Rules From a JSON Schema

Read article
Laravel Suspicious Login: Detect and Block Risky Logins image

Laravel Suspicious Login: Detect and Block Risky Logins

Read article
Decide with Jev: Make Failed AI Requests Safe in Laravel image

Decide with Jev: Make Failed AI Requests Safe in Laravel

Read article