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.

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

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

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
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant 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
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
Rector logo

Rector

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

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Asymmetric Property Visibility in PHP 8.4 image

Asymmetric Property Visibility in PHP 8.4

Read article
Access Laravel Pulse Data as a JSON API image

Access Laravel Pulse Data as a JSON API

Read article
Laravel Forge adds Statamic Integration image

Laravel Forge adds Statamic Integration

Read article
Transform Data into Type-safe DTOs with this PHP Package image

Transform Data into Type-safe DTOs with this PHP Package

Read article
PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell image

PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell

Read article
Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3 image

Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3

Read article