News

Singleton and Scoped Container Attributes in Laravel 12.21

Published
Singleton and Scoped Container Attributes in Laravel 12.21 image

Release updates

Never miss a Laravel release

Get an email whenever a new Laravel release lands.

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.

Sponsored

laravelcloud logo
Laravel Cloud

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

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article