Back to Blog

Laravel 9.51 Released

Julian Beaujardin
Julian Beaujardin Reference: Laravel News February 8th, 2023

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 - 2023-02-07

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)