Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy 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
Jump24 - UK Laravel Agency

Laravel Developers that Click into Place. Never outsourced. Never offshored. Always exceptional.

Visit Jump24 - UK Laravel Agency
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
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 →
FrankenPHP v1.11.2 Released With 30% Faster CGO, 40% Faster GC, and Security Patches image

FrankenPHP v1.11.2 Released With 30% Faster CGO, 40% Faster GC, and Security Patches

Read article
Capture Web Page Screenshots in Laravel with Spatie's Laravel Screenshot image

Capture Web Page Screenshots in Laravel with Spatie's Laravel Screenshot

Read article
Nimbus: An In-Browser API Testing Playground for Laravel image

Nimbus: An In-Browser API Testing Playground for Laravel

Read article
Laravel 12.51.0 Adds afterSending Callbacks, Validator whenFails, and MySQL Timeout image

Laravel 12.51.0 Adds afterSending Callbacks, Validator whenFails, and MySQL Timeout

Read article
Handling Large Datasets with Pagination and Cursors in Laravel MongoDB image

Handling Large Datasets with Pagination and Cursors in Laravel MongoDB

Read article
Driver-Based Architecture in Spatie's Laravel PDF v2 image

Driver-Based Architecture in Spatie's Laravel PDF v2

Read article