Never Miss a 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; // BeforePost::whereRaw('? between "visible_from" and "visible_to"', now())->get();Post::where('visible_from', '<=', now())->where('visible_to', '>=', now())->get(); // AfterPost::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:
// BeforeStr::of(Uri::of('http://localhost')->withScheme('https')); // AfterUri::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; // NumericValidator::make(['foo' => '1'], ['foo' => 'numeric:strict']); // failsValidator::make(['foo' => 1], ['foo' => 'numeric:strict']); // passes // BooleanValidator::make(['foo' => true], ['foo' => 'boolean:strict']); // passesValidator::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(); // truenew 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
- fix(vite): #55793 add explicit as-script to link tag for script modul… by @midsonlajeanty in https://github.com/laravel/framework/pull/55794
- [12.x] Allow globally disabling Factory parent relationships via
Factory::dontExpandRelationshipsByDefault()by @cosmastech in https://github.com/laravel/framework/pull/56154 - [12.x] Adds checking if a value is between two columns by @DarkGhostHunter in https://github.com/laravel/framework/pull/56119
- [12.x] Ensure database connection is always restored by @xurshudyan in https://github.com/laravel/framework/pull/56258
- [12.x] Fix handling of
Htmlableobjects inJs::convertDataToJavaScriptExpression()by @jj15asmr in https://github.com/laravel/framework/pull/56253 - Reduce meaningless intermediate variables. by @LjjGit in https://github.com/laravel/framework/pull/56265
- [12.x] Improve typehints for
AbstractCursorPaginator@through()by @cosmastech in https://github.com/laravel/framework/pull/56267 - Use
Datefacade instead oftime()forpassword_confirmed_atcheck by @dylanbr in https://github.com/laravel/framework/pull/56270 - [12.x] fix: Collection::transform() and Paginator::through() return types by @calebdw in https://github.com/laravel/framework/pull/56273
- [12.x] Merge 11.x into 12.x by @u01jmg3 in https://github.com/laravel/framework/pull/56289
- [12.x] Reduce meaningless intermediate variables by @AhmedAlaa4611 in https://github.com/laravel/framework/pull/56288
- [12.x] Refactor build Method to Use Null Coalescing Assignment for Default C… by @Ashot1995 in https://github.com/laravel/framework/pull/56283
- [12.x] minor code formatting improvements by @browner12 in https://github.com/laravel/framework/pull/56296
- [12.x] Use more specific route binding exception message for child routes by @jessekoerhuis in https://github.com/laravel/framework/pull/56298
- [12.x] Fix Possible Undefined Variables by @calfc in https://github.com/laravel/framework/pull/56292
- [12.x] Fix: Ensure scheduler
dailyAt()method parses minutes and ignores seconds when seconds are provided by @amirhshokri in https://github.com/laravel/framework/pull/56308 - [12.x] Allows for strict boolean validation by @peterfox in https://github.com/laravel/framework/pull/56313
- Improve
SeedCommandconsole output by @Jehong-Ahn in https://github.com/laravel/framework/pull/56310 - [12.x] Add unified enum support across framework docs by @amirhshokri in https://github.com/laravel/framework/pull/56271
- [12.x] Allows for strict numeric validation by @peterfox in https://github.com/laravel/framework/pull/56328
- [12.x] Update PHPDoc annotations in
Validationby @mrvipchien in https://github.com/laravel/framework/pull/56321 - [12.x] Add operator class support for PostgreSQL GiST spatial indexes by @joteejotee in https://github.com/laravel/framework/pull/56324
- Fix multipart array value parsing in HTTP client (#55732) by @joteejotee in https://github.com/laravel/framework/pull/56302
- Fixes bug with ShouldBeUniqueUntilProcessing locks getting stuck due to Middleware by @TWithers in https://github.com/laravel/framework/pull/56318
- [12.x] add prompts based expectations to PendingCommand by @BinaryKitten in https://github.com/laravel/framework/pull/56260
- [12.x] Add Singleton and Scoped attributes to Container by @riasvdv in https://github.com/laravel/framework/pull/56334
- Fix unsetting model castable attribute when cast to object (#56335) by @guram-vashakidze in https://github.com/laravel/framework/pull/56343
- [12.x] Fix/memory improvement by @CharrafiMed in https://github.com/laravel/framework/pull/56345
- [12.x] Add hasMailer method to the mailable class by @kevinb1989 in https://github.com/laravel/framework/pull/56340
- [12.x] Consistent use of
mb_split()to split strings into words by @shaedrich in https://github.com/laravel/framework/pull/56338 - [12.x] Add toStringable to Uri by @Kyrch in https://github.com/laravel/framework/pull/56359
- [12.x] Fix PHPStan Integrations by @crynobone in https://github.com/laravel/framework/pull/56369
- Add 'isEmpty' and 'isNotEmpty' to Fluent by @cworreschk in https://github.com/laravel/framework/pull/56370
- [12.x] Add mergeMetadata method to the Mailable class by @kevinb1989 in https://github.com/laravel/framework/pull/56376
- Add 'dontReportUsing' to filter exceptions to be reported by @pelmered in https://github.com/laravel/framework/pull/56361