Laravel Idea for PhpStorm - Full-featured IDE for productive artisans!

Database Cache Locking and Same-Session Concurrency Restrictions in Laravel 7.10

Published on by

Database Cache Locking and Same-Session Concurrency Restrictions in Laravel 7.10 image

The Laravel team released v7.10.0 with cache lock support for the database driver, same-session ID request concurrency limiting and simplified truth-test assertions. Also, 7.10 includes quite a few new features, along with all the latest fixes and changes in the 7.x branch.

Truth-test Assertions with Just a Closure

Sjors Ottjes contributed a backward-compatible way of writing truth-test assertions with only a type-hinted closure:

// Before
Mail::assertQueued(SubpictureFinishedEmail::class, function (SubpictureFinishedEmail $email) use ($user) {
return $email->user->id === $user->id;
});
 
// With 7.10+
Mail::assertQueued(function (SubpictureFinishedEmail $email) use ($user) {
return $email->user->id === $user->id;
});
 
// or with a short closure
 
Mail::assertQueued(
fn (SubpictureFinishedEmail $email) => $email->user->id === $user->id
);

Same-session ID Request Concurrency Limiting

Taylor Otwell contributed a long-requested feature around session concurrency:

This PR implements a long-requested feature (since basically the beginning of Laravel) for blocking concurrency requests from the same session until previous requests have finished. The basic use case being prevention of concurrent requests writing conflicting data to the session and data being lost.

Be sure to check out 7.x Same-session ID request concurrency limiting · Pull Request #32636 for full explanation and details.

Pagination Count as a Subquery

Taylor Otwell contributed Run pagination count as a subquery for group by and havings:

Maginating queries with groupBy or having statements is a long-standing issue in Laravel going back to the very beginning of the framework with literal dozens of raised issues…This solution was suggested @acasar years ago but I wrote it off at the time – but honestly I think it’s a lot better than what we have now so I’m bringing it up again for consideration.

A cast:make Command

Adrien Leloup contributed an artisan make:cast console command, which takes the name of a cast and saves it in app/Casts. It generates a class that implements the CastsAttributes interface.

Assert Database Count

Christoph Rumpel contributed a test assertion for database table counting:

$this->assertDatabaseCount('users', 50);
 
// A failure would look like:
// "Failed asserting that table [users] matches expected entries count of 50. Entries found: 1."

Auth Mode for the SMTP Mail Driver

@fragkp contributed an auth_mode SMTP driver option:

Sometimes, it’s useful to set explicit the auth_mode when you do not want to handle it by itself.

Possible values are: null, plain, login or cram-md5.

Add hasNamedScope to the Base Model

Alex Bowers and Graham Campbell contributed separate PRs around model scopes.

First, Alex contributed a hasScope method on the base model:

The hasScope method cleans up checking if a scope exists in a dynamic way…The use case for this is requests containing an array of filters to be applied (via scopes), the code is more expressive with:

public function index(Request $request)
{
$post = Post::query();
 
foreach ($request->get('filters', []) as $filter) {
if ($post->hasNamedScope($filter)) {
$post->{$filter}();
}
}
 
return $post->paginate();
}

Note that the method name changed to hasNamedScope in the final 7.10 release with Graham’s PR:

  1. The model is the source of truth for if a scope exists, but then the builder bypasses it to call the scope method. Instead, we should delegate to the model. This PR addresses this, and provides a reusable way to apply scopes.
  2. #32622 added hasScope, but the name hasNamedScope would be more consistent, since callScope expects a full on callable and callNamedScope expects the actual scope name.

Cache Lock Support for the Database Cache Driver

Taylor Otwell contributed cache lock support for the database driver inspired by Symfony’s Lock Component PDOStore. Check out the Pull Request #32639 for implementation details.

Release Notes

While we didn’t look at all the new features in this post, you can see the full list of new features and updates below and the diff between 7.9.0 and 7.10.0 on GitHub. This release has some impressive new features! You can see them all in the latest v7 changelog:

v7.10.0

Added

  • Added artisan make:cast command (#32594)
  • Added Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase::assertDatabaseCount() (#32597)
  • Allow configuring the auth_mode for SMTP mail driver (#32616)
  • Added hasNamedScope() function to the Base Model (#32622, #32631)
  • Allow doing truth-test assertions with just a closure (#32626, f69ad90, 22d6fca)
  • Run pagination count as subquery for group by and havings (#32624)
  • Added Callbacks with Output to Console Schedule (#32633, 35a7883, 8d8d620)
  • Added Cache::lock() support for the database cache driver (#32639, 573831b)
  • Same-session ID request concurrency limiting (#32636)
  • Add skipUntil and skipWhile methods to the collections (#32672, #32676)
  • Support delete with limit on sqlsrv (f16d325)
  • Added mergeFillable() and mergeGuarded() to Model (#32679)

Fixed

  • Prevents a memory leak in Faker (2228233)
  • Fixed setting component name and attributes (#32599, f8ff3ca)
  • Fixed Illuminate\Foundation\Testing\TestResponse::assertSessionHasInput() (f0639fd)
  • Set relation connection on eager loaded MorphTo (#32602)
  • Filtering null’s in hasMorph() (#32614)
  • Fixed Illuminate\Foundation\Console\EventMakeCommand::alreadyExists() (7bba4bf)
  • Fixed Illuminate\Console\Scheduling\Schedule::compileParameters() (cfc3ac9, 36e215d)
  • Fixed bug with model name in Illuminate\Database\Eloquent\RelationNotFoundException::make() (f72a166)
  • Allow trashed through parents to be included in has many through queries (#32609)

Changed

  • Changed Illuminate/Database/Eloquent/Relations/Concerns/AsPivot::fromRawAttributes() (6c502c1)
  • Restore оnly common relations (#32613, d82f78b, 48e4d60)
  • Use single space if plain email is empty in Illuminate\Mail\Mailer::addContent() (0557622)
  • Remove wasted file read when loading package manifest in Illuminate\Foundation\PackageManifest::getManifest() (#32646)
  • Do not change character and collation for some columns on change (fccdf7c)
  • Use table name when resolving has many through / one relationships (8d69454)
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