Database Cache Locking and Same-Session Concurrency Restrictions in Laravel 7.10
Published on by Paul Redmond
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:
// BeforeMail::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
orhaving
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:
- 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.
- #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
andskipWhile
methods to the collections (#32672, #32676) - Support delete with limit on sqlsrv (f16d325)
- Added
mergeFillable()
andmergeGuarded()
toModel
(#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
andcollation
for some columns on change (fccdf7c) - Use table name when resolving has many through / one relationships (8d69454)