4,000 emails/month for free | Mailtrap sends real emails now!

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
Acquaint Softtech

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

Visit Acquaint Softtech
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
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
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 →
Laracon AU Returns to Brisbane - Call for Speakers Now Open image

Laracon AU Returns to Brisbane - Call for Speakers Now Open

Read article
Detecting and Fixing Race Conditions in Laravel Applications image

Detecting and Fixing Race Conditions in Laravel Applications

Read article
LaraCopilot: Generate Laravel MVPs From a Single Prompt With AI image

LaraCopilot: Generate Laravel MVPs From a Single Prompt With AI

Read article
Model::withoutRelation() in Laravel 12.54.0 image

Model::withoutRelation() in Laravel 12.54.0

Read article
Tyro Checkpoint: Instant SQLite Snapshots for Laravel Local Development image

Tyro Checkpoint: Instant SQLite Snapshots for Laravel Local Development

Read article
The Laravel Community Mobile App Helps You Discover Events and Connect With Developers image

The Laravel Community Mobile App Helps You Discover Events and Connect With Developers

Read article