Laravel 9.7 Released

News

April 6th, 2022

Laravel 9.7 Released

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:

1// Previously
2$query
3 ->whereBelongsTo($category[0])
4 ->orWhereBelongsTo($category[1])
5 // ...
6 
7// Or...
8$query->whereIn('category_id', $categories->modelKeys());
9 
10 
11// >=9.7 can use collections:
12$query->whereBelongsTo($categories);
13$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:

1DB::table('json_table')
2 ->where('column->json_option[0]', 'foo')
3 ->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:

1use Illuminate\Routing\Events\Routing;
2 
3Event::listen(function (Routing $event) {
4 // ...
5});

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:

1Route::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.

1use App\Jobs\ImportCsv;
2use Illuminate\Bus\Batch;
3use Illuminate\Support\Facades\Bus;
4 
5$batch = Bus::batch([
6 (new ImportCsv(1, 100))->delay($delay),
7 (new ImportCsv(101, 200))->delay($delay)
8])->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:

1$this->assertSame(
2 'laravel php framework',
3 Str::squish(' laravel php framework '));
4 
5$this->assertSame(
6 'laravel php framework',
7 Str::squish("laravel\t\tphp\n\nframework")
8);
9 
10$this->assertSame(
11 'laravel php framework',
12 Str::squish('
13 laravel
14 php
15 framework
16 ')
17);

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:

1DB::table('users')
2 ->whereJsonContainsKey('options->languages')
3 ->get();
4 
5DB::table('users')
6 ->whereJsonDoesntContainKey('options->language->primary')
7 ->get();
8 
9DB::table('users')
10 ->whereJsonContainsKey('options->2fa[0]')
11 ->get();
12 
13DB::table('users')
14 ->whereJsonDoesntContainKey('options->2fa[0][1]')
15 ->get();

Dispatch Batch After a Response

OMAR.A contributed the ability to dispatch a batch after the response is sent to the user:

1$batch = Bus::batch([
2 new ImportCsv(1, 100),
3 new ImportCsv(101, 200),
4 new ImportCsv(201, 300),
5 new ImportCsv(301, 400),
6 new ImportCsv(401, 500),
7])->then(function (Batch $batch) {
8 // All jobs completed successfully...
9})->catch(function (Batch $batch, Throwable $e) {
10 // First batch job failure detected...
11})->finally(function (Batch $batch) {
12 // The batch has finished executing...
13})->dispatchAfterResponse();
14 
15// also, it returns a batch object so you can access batch id
16return $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)

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.