Laravel 9.7 Released

Published on by

Laravel 9.7 Released image

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 id
return $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)
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
Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Forge
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
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
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a 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
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Microsoft Clarity Integration for Laravel image

Microsoft Clarity Integration for Laravel

Read article
Apply Dynamic Filters to Eloquent Models with the Filterable Package image

Apply Dynamic Filters to Eloquent Models with the Filterable Package

Read article
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article
Asserting Exceptions in Laravel Tests image

Asserting Exceptions in Laravel Tests

Read article
Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4 image

Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4

Read article
Basset is an alternative way to load CSS & JS assets image

Basset is an alternative way to load CSS & JS assets

Read article