Laravel 9.7 Released
Published on by Paul Redmond
The Laravel team released Laravel v9.7.0 with a whereIn() route parameter constraint method, a Str::squish() helper, JSON path query enhancements, and more:
Query Builder whereBelongsTo() Accepts Collections
Erik Gaal contributed the ability to pass a collection to the whereBelongsTo()
method in a query builder:
// Previously$query ->whereBelongsTo($category[0]) ->orWhereBelongsTo($category[1]) // ... // Or...$query->whereIn('category_id', $categories->modelKeys()); // >=9.7 can use collections:$query->whereBelongsTo($categories);$query->whereBelongsTo($categories, 'category');
Database Queries Containing Json Paths Support Array Index Braces
Derek MacDonald contributed support for array index braces in database queries containing JSON paths:
DB::table('json_table') ->where('column->json_option[0]', 'foo') ->update(['column->json_option[0]', => 'bar']);
See Pull Request #41767 for further details and related PRs/issues.
Routing Event Fires Before Route Matched
Tim Roberson contributed a Routing
event that fires before the router attempts to find a matching route. This event allows developers to access the request immediately before routing:
use Illuminate\Routing\Events\Routing; Event::listen(function (Routing $event) { // ...});
Route whereIn() Parameter Constraint Method
@Propaganistas contributed a whereIn()
route parameter constraint method, which can be used to match a route param against an array of allowed values:
Route::get('/foo/{bar}')->whereIn('bar', $values);
Batch Job Delay for Beanstalkd and SQS Queues
OMAR.A contributed to the ability to use batch jobs delay. Before these PR contributions, SQS and Beanstalkd ignored delay time when sending messages to the queue, and now, they consider the delay as expected.
use App\Jobs\ImportCsv;use Illuminate\Bus\Batch;use Illuminate\Support\Facades\Bus; $batch = Bus::batch([ (new ImportCsv(1, 100))->delay($delay), (new ImportCsv(101, 200))->delay($delay)])->dispatch();
String Squish Helper
Dwight Watson contributed a squish()
string helper to remove all "extra" blank space from a given string. Here are some examples from the pull request tests to get a visual of what squish does:
$this->assertSame( 'laravel php framework', Str::squish(' laravel php framework ')); $this->assertSame( 'laravel php framework', Str::squish("laravel\t\tphp\n\nframework")); $this->assertSame( 'laravel php framework', Str::squish(' laravel php framework '));
Query Builder "whereJsonContainsKey()" Method
Derek MacDonald contributed a whereJsonContainsKey()
method. It supports checking for array integer keys and supports SQLite. Here are some examples from the pull request description:
DB::table('users') ->whereJsonContainsKey('options->languages') ->get(); DB::table('users') ->whereJsonDoesntContainKey('options->language->primary') ->get(); DB::table('users') ->whereJsonContainsKey('options->2fa[0]') ->get(); DB::table('users') ->whereJsonDoesntContainKey('options->2fa[0][1]') ->get();
Dispatch Batch After a Response
OMAR.A contributed the ability to dispatch a batch after the response is sent to the user:
$batch = Bus::batch([ new ImportCsv(1, 100), new ImportCsv(101, 200), new ImportCsv(201, 300), new ImportCsv(301, 400), new ImportCsv(401, 500),])->then(function (Batch $batch) { // All jobs completed successfully...})->catch(function (Batch $batch, Throwable $e) { // First batch job failure detected...})->finally(function (Batch $batch) { // The batch has finished executing...})->dispatchAfterResponse(); // also, it returns a batch object so you can access batch idreturn $batch->id;
Release Notes
You can see the complete list of new features and updates below and the diff between 9.6.0 and 9.7.0 on GitHub. The following release notes are directly from the changelog:
v9.7.0
Added
- Make whereBelongsTo accept Collection (#41733)
- Database queries containing JSON paths support array index braces (#41767)
- Fire event before route matched (#41765)
- Added to
Illuminate/Http/Resources/ConditionallyLoadsAttributes::whenNotNull
method (#41769) - Added "whereIn" route parameter constraint method (#41794)
- Added
Illuminate/Queue/BeanstalkdQueue::bulk()
(#41789) - Added
Illuminate/Queue/SqsQueue::bulk()
(#41788) - Added String::squish() helper (#41791)
- Added query builder method whereJsonContainsKey() (#41802)
- Enable dispatchAfterResponse for batch (#41787)
Fixed
- Factory generation fixes (#41688)
- Fixed Http Client throw boolean parameter of retry method (#41762, #41792)
- Ignore empty redis username string in PhpRedisConnector (#41773)
- Fixed support of nullable type for AsArrayObject/AsCollection (#41797, 05846e7)
- Fixed adding jobs from iterable to the pending batch (#41786)
- Http client: fix retry handling of connection exception (#41811)
Changed
- Enable batch jobs delay for database queue (#41758)
- Enable batch jobs delay for redis queue (#41783)
- Http client: dispatch "response received" event for every retry attempt (#41793)
- Http Client: provide pending request to retry callback (#41779)
- Allow non length limited strings and char for postgresql (#41800)
- Revert some Carbon::setTestNow() removals (#41810)
- Allow cleanup of databases when using parallel tests (#41806)