The Laravel team released v11.42, which includes relative date helpers, fluent numeric validation, JSON assertions on streamed content, and more.
Relative Date Shorthands
Jason McCreary contributed relative date shorthand methods to the query builder that you can use to constrain date fields for things like finding records in the past, future, today, after today, and more:
DB::table('invoices') ->wherePast('due_at') ->get(); DB::table('invoices') ->whereFuture('due_at') ->get(); DB::table('invoices') ->whereNowOrPast('due_at') ->get(); DB::table('invoices') ->whereNowOrFuture('due_at') ->get(); DB::table('invoices') ->whereToday('due_at') ->get(); DB::table('invoices') ->whereBeforeToday('due_at') ->get(); DB::table('invoices') ->whereAfterToday('due_at') ->get();
Besides the above methods shown, they also include or
and not
variants and the ability to pass an array of column names:
All of these methods have their
where
,or
, andorWhereNot
counterparts. All methods also accept an array of column names as the first argument. ThewherePast
andwhereFuture
have an optional second argument to set$now
.
See Pull Request #54408 for implementation details and test examples.
Fluent Numeric Validation
Carlos Junior contributed a fluent numeric validation class, providing an expressive way to define numeric validation rules:
// Before$rules = [ 'score' => 'numeric|integer|multiple_of:10|lte:some_field|max:100',]; // After$rules = [ 'score' => [ Rule::numeric() ->integer() ->multipleOf(10) ->lessThanOrEqual('some_field') ->max(100); ],];
Context "missing" Method
Vincent Bergeron contributed a missing()
and missingHidden()
method to the Context service, which returns a boolean if a context key is missing:
Context::add('url', $request->url());Context::addHidden('hidden_url', $request->url()); Context::missing('url'); // falseContext::missing('missing_key'); // true Context::missingHidden('url'); // trueContext::missingHidden('hidden_url'); // false
JSON Assertions on Streamed Content
Günther Debrauwer contributed the ability to use JSON assertions on a streamed JSON response:
Route::get('/users', function () { return response()->streamJson([ 'data' => User::cursor(), ]);}); $this->getJson('/users') ->assertJsonCount(10, 'data') ->assertJsonPath('data.*.id', $users->pluck('id')->all());
Release notes
You can see the complete list of new features and updates below and the diff between 11.41.0 and 11.42.0 on GitHub. The following release notes are directly from the changelog:
v11.42.0
- docs: clarify use of hasOption() by @jezmck in https://github.com/laravel/framework/pull/54415
- Test Improvements by @crynobone in https://github.com/laravel/framework/pull/54427
- [11.x] add Generics to Paginator's ArrayAccess methods by @taka-oyama in https://github.com/laravel/framework/pull/54428
- [11.x] Fix docblocks for code that calls
enum_value()
by @cosmastech in https://github.com/laravel/framework/pull/54432 - [11.x] Fix assertContent on laravel test that respond with Symfony Response Object by @tben in https://github.com/laravel/framework/pull/54467
- [11.x] Add Higher Order Messaging support for last by @fernandokbs in https://github.com/laravel/framework/pull/54459
- [11.x] Database testing traits has impact to artisan calls by @nivseb in https://github.com/laravel/framework/pull/54458
- [11.x] Add precision to
Number::currency()
by @benjibee in https://github.com/laravel/framework/pull/54456 - [11.x] Add generics to lazy queries by @axlon in https://github.com/laravel/framework/pull/54453
- [11.x] Merge in eager loads from nested where queries by @ollieread in https://github.com/laravel/framework/pull/54455
- [11.x] Fluent numeric validation by @xoesae in https://github.com/laravel/framework/pull/54425
- [11.x] Fix casts +
withAttributes
by @tontonsb in https://github.com/laravel/framework/pull/54422 - [11.x] Ensure batched jobs are actually batchable by @josepostiga in https://github.com/laravel/framework/pull/54442
- [11.x] Update PHPStan to 2.x by @tamiroh in https://github.com/laravel/framework/pull/53716
- Test Improvements by @crynobone in https://github.com/laravel/framework/pull/54475
- Add relative date shorthands to Query Builder by @jasonmccreary in https://github.com/laravel/framework/pull/54408
- [11.x] feat: add better closure typing in QueriesRelationships by @calebdw in https://github.com/laravel/framework/pull/54452
- [11.x] Fix the method explodeExplicitRule to support Numeric Validation by @mrvipchien in https://github.com/laravel/framework/pull/54478
- Add
Builder
On Clone callback support by @ralphjsmit in https://github.com/laravel/framework/pull/54477 - Support relative paths to SQLite databases by @LukeTowers in https://github.com/laravel/framework/pull/54480
- [11.x] Where doesnt have nullable morph by @liamduckett in https://github.com/laravel/framework/pull/54363
- [11.x] Add the ability to skip migrations within tests by @cosmastech in https://github.com/laravel/framework/pull/54441
- Queue Integration Tests with Redis Cluster by @vadimonus in https://github.com/laravel/framework/pull/54218
- [11.x] Optimize
PendingBatch@ensureJobIsBatchable
by @cosmastech in https://github.com/laravel/framework/pull/54485 - [11.x] Supports PHPUnit 12.0 by @crynobone in https://github.com/laravel/framework/pull/54316
- [11.x] Fix spelling in comment by @lorenzolosa in https://github.com/laravel/framework/pull/54503
- [11.x] Add Context "missing" method by @vbergerondev in https://github.com/laravel/framework/pull/54499
- [11.x] feat: add generics to Container methods by @MrMeshok in https://github.com/laravel/framework/pull/54543
- [11.x] Add a setAssetRoot method to the UrlGenerator class by @ollieread in https://github.com/laravel/framework/pull/54530
- [11.x] Handle Null Check in Str::startsWith and Str::endsWith by @onairmarc in https://github.com/laravel/framework/pull/54520
- [11.x] Improve check for relative sqlite databases by @LukeTowers in https://github.com/laravel/framework/pull/54513
- Revert "[11.x] Use Str::wrap() instead of nesting Str::start() inside Str::finish()" by @shaedrich in https://github.com/laravel/framework/pull/54528
- [11.x] Job Batches with Redis Cluster by @vadimonus in https://github.com/laravel/framework/pull/54522
- [11.x] fix: specify type of TClass generic in Container by @MrMeshok in https://github.com/laravel/framework/pull/54545
- [11.x] Improve docblocks for morph maps in
Relation
by @cosmastech in https://github.com/laravel/framework/pull/54560 - docs: fix return type documentation for initializeSignal method by @nzsys in https://github.com/laravel/framework/pull/54553
- [11.x] Add support for middlewares & failed handler on broadcastable events by @Jacobs63 in https://github.com/laravel/framework/pull/54562
- [11.x] json assertions on streamed content by @gdebrauwer in https://github.com/laravel/framework/pull/54565