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

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
Laravel Cloud

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

Visit Laravel Cloud
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
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 Boost v2.0 Released with Skills Support image

Laravel Boost v2.0 Released with Skills Support

Read article
Laravel Debugbar v4.0.0 is released image

Laravel Debugbar v4.0.0 is released

Read article
Radiance: Generate Deterministic Mesh Gradient Avatars in PHP image

Radiance: Generate Deterministic Mesh Gradient Avatars in PHP

Read article
Speeding Up Laravel News With Cloudflare image

Speeding Up Laravel News With Cloudflare

Read article
Livewire 4 Support in Laravel VS Code Extension v1.4.3 image

Livewire 4 Support in Laravel VS Code Extension v1.4.3

Read article
Fair Queue Distribution with Laravel Balanced Queue image

Fair Queue Distribution with Laravel Balanced Queue

Read article