See rates for the top Laravel developers in Latin America

Singleton and Scoped Container Attributes in Laravel 12.21

Last updated on by

Singleton and Scoped Container Attributes in Laravel 12.21 image

Never Miss a Laravel Release 🚀

Sign up and get an email with each new Laravel release

The Laravel team released v12.21.0 with singleton and scoped container attributes, a query builder method to check if a value is between two columns using the query builder, strict numeric and boolean validation options, and more:

Singleton and Scoped Container Attributes

@riasvdv contributed two PHP attributes that can be added directly to a class you're resolving through the container, without manually registering them as a scoped or singleton instance:

use Illuminate\Container\Attributes\Singleton;
use Illuminate\Container\Attributes\Scoped;
 
#[Singleton]
class MyService
{
}
 
#[Scoped]
class MyService
{
}

Check out the singleton and scoped singleton container docs for details on how these concepts work in Laravel's service container.

Check if a Value is Between to Columns with the Query Builder

@DarkGhostHunter contribued the whereValueBetween() method to check if a value is between two columns using the query builder:

This PR allows the developer to check if a given value is between two columns through the Query Builder.

This complements both whereBetween() whereColumnsBetween() methods, as these can't check if a value (like an integer or timestamp) is between two columns. It's left to the developer to properly cast the values or column values as with the prior methods.

use App\Models\Post;
 
// Before
Post::whereRaw('? between "visible_from" and "visible_to"', now())->get();
Post::where('visible_from', '<=', now())->where('visible_to', '>=', now())->get();
 
// After
Post::whereValueBetween(now(), ['visible_from', 'visible_to'])->get();

Here are the four related query builder methods in Pull Request #56119:

  • whereValueBetween()
  • orWhereValueBetween()
  • whereValueNotBetween()
  • orWhereValueNotBetween()

Allow Globally Disabling Factory Relationships

Luke Kuzmish contributed the ability to disable creating parent relationships in factories by default. You can use this behavior to ensure that no relationship data is created. To use this feature, you can call dontExpandRelationshipsByDefault() and expandRelationshipsByDefault() static methods on a factory to toggle this behavior:

public function test_has_one_editor_permission_returns_true(): void
{
UserPermissionFactory::dontExpandRelationshipsByDefault();
 
$collection = new UserPermissionCollection([
UserPermission::factory()
->withoutParents()
->make([
'type' => 'viewer',
'company_id' => 2,
'product_id' => 789,
]),
UserPermission::factory()
->withoutParents()
->make([
'type' => 'editor',
'company_id' => 2,
'product_id' => 432,
]),
]);
 
$result = $collection->hasEditorForCompany(2);
 
$this->assertTrue($result);
}

See Pull Request #56154 for details.

Get a URI as a Stringable Instance

@Kyrch contributed a toStringable() method to get a URI as a Stringable instance:

// Before
Str::of(Uri::of('http://localhost')->withScheme('https'));
 
// After
Uri::of('http://localhost')->withScheme('https')->toStringable();

Strict Numeric and Boolean Validation

Peter Fox contributed strict numeric and boolean validation, which will additionally check the type of the value under validation:

use Illuminate\Support\Facades\Validator;
 
// Numeric
Validator::make(['foo' => '1'], ['foo' => 'numeric:strict']); // fails
Validator::make(['foo' => 1], ['foo' => 'numeric:strict']); // passes
 
// Boolean
Validator::make(['foo' => true], ['foo' => 'boolean:strict']); // passes
Validator::make(['foo' => '1'], ['foo' => 'boolean:strict']); // fails

The documentation has been updated for boolean and numeric rules.

Fluent Empty Check Methods

Christian Worreschk contributed isEmpty() and isNotEmpty() methods to the Fluent class to determine if the instance is empty or not:

$fluent = new Fluent([
'name' => 'Laravel News',
'url' => 'https://laravel-news.com',
]);
 
$fluent->isEmpty(); // false
$fluent->isNotEmpty(); // true
 
new Fluent()->isEmpty(); // true
new Fluent()->isNotEmpty(); // false

Release notes

You can see the complete list of new features and updates below and the diff between 12.20.0 and 12.21.0 on GitHub. The following release notes are directly from the changelog:

v12.21.0

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
CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and more

Visit CodeRabbit
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
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
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
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
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
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
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 12.44 Adds HTTP Client afterResponse() Callbacks image

Laravel 12.44 Adds HTTP Client afterResponse() Callbacks

Read article
Handle Nested Data Structures in PHP with the Data Block Package image

Handle Nested Data Structures in PHP with the Data Block Package

Read article
Detect and Clean Up Unchanged Vendor Files with Laravel Vendor Cleanup image

Detect and Clean Up Unchanged Vendor Files with Laravel Vendor Cleanup

Read article
Seamless PropelAuth Integration in Laravel with Earhart image

Seamless PropelAuth Integration in Laravel with Earhart

Read article
Laravel API Route image

Laravel API Route

Read article
Laravel News 2025 Recap image

Laravel News 2025 Recap

Read article