Outsource Laravel Development Partner - $3200/Month | Bacancy

Laravel 9.51 Released

Published on by

Laravel 9.51 Released image

Never Miss a Laravel Release 🚀

Sign up and get an email with each new Laravel release

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
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review
Bacancy logo

Bacancy

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

Bacancy
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
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
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

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

Lucky Media
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

The latest

View all →
Encrypt Files in Laravel with AES-256-GCM and Memory-Efficient Streaming image

Encrypt Files in Laravel with AES-256-GCM and Memory-Efficient Streaming

Read article
Statamic 6 Is Officially Released image

Statamic 6 Is Officially Released

Read article
Livewire 4 and Blade Improvements in Laravel VS Code Extension v1.5.0 image

Livewire 4 and Blade Improvements in Laravel VS Code Extension v1.5.0

Read article
Manage PostgreSQL Databases Directly in VS Code with Microsoft's Extension image

Manage PostgreSQL Databases Directly in VS Code with Microsoft's Extension

Read article
NativePHP for Mobile Is Now Free image

NativePHP for Mobile Is Now Free

Read article
Fuse for Laravel: A Circuit Breaker Package for Queue Jobs image

Fuse for Laravel: A Circuit Breaker Package for Queue Jobs

Read article