Tinkerwell - The PHP Scratchpad

Laravel 5.7.10 Released

Published on by

Laravel 5.7.10 Released image

The Laravel team shipped Laravel 5.7.10 yesterday with a handful of new features, fixes, and changes:

loadCount Eloquent Collection Method

First up, is the ability to load relationship counts on an Eloquent collection. Before this feature, you could only load relationships, but now you can call loadCount() to get counts for all relations.

The pull request illustrates how you could use loadCount() with the following example:

$events = Event::latest()->with('eventable')->paginate();
 
$groups = $events->map(function ($event) {
return $event->eventable;
})->groupBy(function ($eventable) {
return get_class($eventable);
});
 
$groups[Post::class]->loadCount('comments');
$groups[Comment::class]->loadCount('hearts');
 
return new EventIndexResponse($events);

Now all the eventable models would have a count value for comments or hearts respectively. The loadCount feature was added by Tim MacDonald. Tim is the author of the Laravel Log Fake package that we featured earlier this month. Nice work Tim!

Pass a Model to assertSoftDeleted

You can use the assertSoftDeleted to assert that a model has been soft deleted. Before Laravel 5.7.10, the method call might look something like the following:

$this->assertSoftDeleted('users', [
'id' => $user->id,
'name' => $user->name,
'email' => $deletedUser->email,
]);

Now, thanks to an update to the assertSoftDeleted method, you can pass the model directly which will assert against the table’s primary key and primary key value:

$this->assertSoftDeleted($user);
 
// Same as:
$this->assertSoftDeleted('users', ['id' => $user->id]);

Thanks to Dwight Watson for this nice shortcut!

Add Mock and Spy Objects to the Container

Titas Gailius added the ability to more conveniently add mock and spy instances to the container in a test:

$this->mock(Mailer::class, function ($mock) {
$mock->shouldReceive('send')->once();
});
 
 
$this->spy(Dispatcher::class, function ($mock) {
$mock->shouldReceive('dispatch')->andReturn(false);
});

These methods are syntactic sugar for something like the following:

$this->instance(Mailer::class, Mockery::mock(Mailer::class, function ($mock) {
$mock->shouldReceive('send')->once();
}));

UUID Validation

André Filipe contributed a UUID validation method which uses a regex to avoid dependency of ramsey/uuid in the illuminate/validation component.

The name of the validation rule is uuid:

$request->validate([
'post' => 'required|uuid'
]);

Notification Fake can Assert a Preferred Locale

Derek MacDonald added the ability to assert the preferred notification fake locale on notifications that implement the HasLocalePreference interface:

Notification::fake();
 
$user = factory(User::class)->create(['locale' => 'au']);
 
// Call an artisan console command that invokes:
Notification::send($user, new AFLTrade('Chad Wingard', 'Hawthorn'));
 
Notification::assertSentTo($user, AFLTrade::class, function ($notification, $channels, $notifiable, $locale) use ($user) {
return $notification->player === 'Chad Wingard' &&
$notification->club === 'Hawthorn' &&
$notifiable === $user &&
$locale === 'au';
});

You can see the full diff between 5.7.9 and 5.7.10 on GitHub, and the full release notes below from the 5.7 changelog:

v5.7.10

Added

  • Added loadCount method to eloquent collections (#25997)
  • Added support for identity columns in PostgreSQL 10+ (#26096)
  • Allowed passing a model instance directly to assertSoftDeleted method in Foundation/Testing/Concerns/InteractsWithDatabase.php (#26133 , #26148)
  • Added possibility to define exclude methods on registered apiResource (#26149)
  • Added filp/whoops to suggest in composer.json (#26180)
  • Added mock and spy methods to Foundation/Testing/Concerns/InteractsWithContainer.php (#26171, b50f9f3)
  • Added uuid validation rule to validator (#26135)
  • NotificationFake can assert preferred locale (#26205)

Fixed

  • Fixed whereHas and $withCount bindings from polymorphic relationships (#26145)
  • Fixed getTable method in Model (#26085)
  • Fixed filesystem locking hangs in PackageManifest::build() (#26010, 98b8256)
  • Fixed Illuminate/Http/Testing/File.php for Symfony 4.1 components (#26080)
  • Fixed URL in Notifications/resources/views/email.blade.php (22ca105)
  • Fixed hasValidSignature method when someone send a null signature in UrlGenerator.php (#26132)
  • Fixed autocomplete for container in ServiceProvider for cases when someone developed packages (#26063)
  • Fixed ColumnDefinition::default typehint (#26041)

Changed

  • Define mix as const in react-stubs/webpack.mix.js and vue-stubs/webpack.mix.js presets (#26119)
  • Make assertSessionHasNoErrors in TestResponse.php print the unexpected errors (#26039, e6bdf8a)
  • Replaced the remaining occurrences of newQuery() to newModelQuery() in UPDATE/DELETE queries. (#26158)
  • Improved findOrFail() exceptions in BelongsToMany.php and HasManyThrough.php relations (#26182)

Changed realization

  • Reversed ternary condition in Arr::wrap to make it clearer (#26150)
  • Simplified formatAction in UrlGenerator.php (#26121)
  • Simplified isChainOfObjects method in Support/Testing/Fakes/QueueFake.php (#26151)
  • Deleted unneeded code (#26053, #26162, #26160, #26159, #26152)
  • Prefer stricter comparison (#26139, #26157)
  • Removed duplicated code from Router::updateGroupStack method (#26206, 6debff6)
Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in:
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 →
Filament v5.2.0 Adds a Callout Component image

Filament v5.2.0 Adds a Callout Component

Read article
OpenAI Releases GPT-5.3-Codex, a New Codex Model for Agent-Style Development image

OpenAI Releases GPT-5.3-Codex, a New Codex Model for Agent-Style Development

Read article
Claude Opus 4.6 adds adaptive thinking, 128K output, compaction API, and more image

Claude Opus 4.6 adds adaptive thinking, 128K output, compaction API, and more

Read article
Laravel Announces Official AI SDK for Building AI-Powered Apps image

Laravel Announces Official AI SDK for Building AI-Powered Apps

Read article
`hasMany()` Collection Method in Laravel 12.50.0 image

`hasMany()` Collection Method in Laravel 12.50.0

Read article
Mask Sensitive Eloquent Attributes on Retrieval in Laravel image

Mask Sensitive Eloquent Attributes on Retrieval in Laravel

Read article