Laravel 9.51 Released

Published on by

Laravel 9.51 Released image

The Laravel team released 9.51 this week with a new database query count expectation, relationship builder for one/many through, HTTP URI templates, and more.

Before we get to the main Laravel framework release, this week also saw some cool new features throughout the Laravel ecosystem. The fact that all this is shipping a week before Laravel 10 still blows my mind!

Laravel Sail add command

Tony Messias contributed a sail:add command that you can use to add new services to an existing Sail installation.

Laravel Dusk database truncation

Patrick O'Meara contributed a DatabaseTruncation trait that will migrate your database and then truncate your tables to speed up rerunning database migrations. See the Dusk Using Database Truncations documentation for further details.

Now, onto what's new in Laravel 9.51:

Database query count test expectation

Tim MacDonald contributed a test expectation for database query count. This expectation can ensure code doesn't inadvertently introduce new database queries:

public function testItWorksEfficiently()
{
$this->expectsDatabaseQueryCount(5);
 
// do stuff...
 
// throws an exception on tearDown if more than 5 queries have run
// since the expectation was created.
}

Pending "has many through" and "has one through" builder

Tim MacDonald contributed a pending builder for has-one-through and has-many-through relationships. The example given in Pull Request #45894 has the following relationships:

  • Project -> Has Many -> Environment
  • Environment -> Has Many -> Deployment

Given the above relationships, here's how you can write a "has many through" relationship with the pending builder:

class Project extends Model
{
public function deployments()
{
return $this->through($this->environments())
->has(fn (Environment $env) => $env->deployments());
}
 
public function environments()
{
return $this->hasMany(Environment::class);
}
}

To illustrate the "has one through" relationship, take the following existing relationships:

  • Mechanic -> Has One -> Car
  • Car -> Has One -> Owner

Given the above relationships, here's how you can write the "has one through" relationship with the pending builder:

class Mechanic extends Model
{
public function owner()
{
return $this->through($this->car())
->has(fn (Car $car) => $car->owner());
}
 
public function car()
{
return $this->hasOne(Car::class);
}
}

See the Has Many Through and the Has One Through documentation for further details on relationships with Laravel.

HTTP client URI templates

James Brooks contributed URI templates to the Laravel HTTP client, which uses guzzlehttp/uri-templates under the hood:

Http::withUrlParameters([
'endpoint' => 'https://laravel.com',
'page' => 'docs',
'version' => '9.x',
'thing' => 'validation',
])->get('{+endpoint}/{page}/{version}/{thing}');
 
// https://laravel.com/docs/9.x/validation

Release Notes

You can see the complete list of new features and updates below and the diff between 9.50.0 and 9.51.0 on GitHub. The following release notes are directly from the changelog:

v9.51.0

Added

  • Added Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase::expectsDatabaseQueryCount() (#45932)
  • Added pending has-many-through and has-one-through builder (#45894)
  • Added Illuminate/Http/Client/PendingRequest::withUrlParameters() (#45982)

Fixed

  • Fix: prevent duplicated content-type on HTTP client (#45960)
  • Add missing php extensions in composer (#45941)

Changed

  • Command schedule:work minor features: schedule:run output file & environment specific verbosity (#45949)
  • Added missing self reserved word to reservedNames array in Illuminate/Console/GeneratorCommand.php (#46001)
  • pass value along to ttl callback in Illuminate/Cache/Repository::remember() (#46006)
  • Make sure the lock_connection is used for schedule's withoutOverlapping() (#45963)
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
Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Visit Paragraph
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 Vue and Livewire 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 →
A New Validation Rule and the Ability to Manually Fail a Command in Laravel 11.8 image

A New Validation Rule and the Ability to Manually Fail a Command in Laravel 11.8

Read article
New Proposed Array Find Functions in PHP 8.4 image

New Proposed Array Find Functions in PHP 8.4

Read article
Laracon AU 2024 tickets are now on sale image

Laracon AU 2024 tickets are now on sale

Read article
Dash UI is a Laravel Blade Component Library Inspired by Shopify Polaris image

Dash UI is a Laravel Blade Component Library Inspired by Shopify Polaris

Read article
A Look at What's Coming to PHP 8.4 image

A Look at What's Coming to PHP 8.4

Read article
Is class instantiation without extra parenthesis coming to PHP 8.4? image

Is class instantiation without extra parenthesis coming to PHP 8.4?

Read article