Get expert guidance in a few days with a Laravel code review

`hasMany()` Collection Method in Laravel 12.50.0

Published on by

`hasMany()` Collection Method in Laravel 12.50.0 image

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.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Laravel Announces Official AI SDK for Building AI-Powered Apps image

Laravel Announces Official AI SDK for Building AI-Powered Apps

Read article
`hasMany()` Collection Method in Laravel 12.50.0 image

`hasMany()` Collection Method in Laravel 12.50.0

Read article
Mask Sensitive Eloquent Attributes on Retrieval in Laravel image

Mask Sensitive Eloquent Attributes on Retrieval in Laravel

Read article
Encrypt Files in Laravel with AES-256-GCM and Memory-Efficient Streaming image

Encrypt Files in Laravel with AES-256-GCM and Memory-Efficient Streaming

Read article
Statamic 6 Is Officially Released image

Statamic 6 Is Officially Released

Read article
Livewire 4 and Blade Improvements in Laravel VS Code Extension v1.5.0 image

Livewire 4 and Blade Improvements in Laravel VS Code Extension v1.5.0

Read article