Laravel 8.16 Released

News

December 1st, 2020

laravel8.jpg

The Laravel team released 8.16 last week with an artisan command to take you directly to a database CLI, an excellent progress bar convenience method, and a new Ably broadcaster implementation.

This release was jam-packed with awesome new features, so let’s check out the latest changes in the 8.x branch:

With Progress Bar Convenience Method

Taylor Otwell contributed the withProgressBar() convenience method you can use in console commands. Here’s what you might do to create a progress bar straight from the docs:

1$users = App\Models\User::all();
2$bar = $this->output->createProgressBar(count($users));
3 
4$bar->start();
5 
6foreach ($users as $user) {
7 $this->performTask($user);
8 
9 $bar->advance();
10}
11 
12$bar->finish();

With this convenience method, it could look like this:

1$users = App\Models\User::all();
2 
3$this->withProgressBar($users, function ($user, $bar) {
4 $this->performTask($user);
5});

Call Silently

Taylor Otwell contributed a callSilently() method which is an alias to callSilent in the CallsCommands trait. You might find that this reads more clearly:

1$this->callSilently('config:cache');

Release Unique Job Locks Before Processing

Paras Malhotra contributed the ability to release unique job locks before the job processes. The new interface is useful for scenarios where a race condition could exist between job completion and unlocking. To learn more, I’d suggest checking out Pull Request #35255 to the framework and the documentation pull request.

Ably Broadcaster

Taylor Otwell contributed an AblyBroadcaster for broadcasting real-time, live-updating user interfaces. Developers can now pick from Pusher, Redis, and Ably for real-time communication needs. For a refresher, check out the Broadcasting documentation on how you can get started broadcasting in your Laravel projects.

Support Delaying Notifications Per Channel

Gergő D. Nagy contributed the ability to delay notification delivery on a per-channel basis instead of one overall delay. Defining delays per channel is useful if you need to delay the same notification differently for email, database, SMS, etc., notifications. You may now optionally call delay() with an associative array of channels you’d like to delay:

1$user->notify((new InvoicePaid($invoice))->delay([
2 'mail' => now()->addMinutes(5),
3 'sms' => now()->addMinutes(10),
4]));

Note that a channel not defined in the array will not have a delay when using the array format.

Allow Array Sorting By Multiple Criteria

johnylemon contributed a sortByMany method to the Arr support class, which will sort props in ascending order by default:

1$sorted = Arr::sortByMany($unsorted, [
2 'name', 'age'
3];
4 
5$sorted = Arr::sortByMany($unsorted, [
6 ['name', false], // will be ordered by `name` descending
7 ['age', true] // then by age ascending, if there are items with same `name` value
8];

Lastly, the PR outlines complex sorting using closures:

1$sorted = Arr::sortByMany($unsorted, [
2 // order by childrens count asc
3 function($a, $b) {
4 return $a->childrens()->count() <=> $b->childrens()->count();
5 },
6 // then by grandchildrens by descending order if needed
7 function($a, $b) {
8 return $b->grandchildrens()->count() <=> $a->grandchildrens()->count();
9 }
10];

Drop Into a Database CLI

Paras Malhotra—who has been contributing a lot of excellent queue features lately—contributed a new artisan command to drop into a database CLI:

1php artisan db mysql

The above command will use the database credentials to connect to a MySQL CLI prompt without manually remembering/alias commands. This command should work with mysql, pgsql, sqlite, and sqlsrv.

Collections splitIn Method

Andrew Brown contributed the splitIn method, which is similar to split. The distinct difference is that split makes it so that each chunk never differs in count from another chunk by more than one. Conversely, splitIn will fill all earlier chunks before allocating the remainder to the final chunk. The pull request has further details on why you might use splitIn if you’d like to learn more.

Release Notes

You can see the full list of new features and updates below and the diff between 8.15.0 and 8.16.0 on GitHub. The following release notes are directly from the changelog:

v8.16.0

Added

  • Added Illuminate\Console\Concerns\InteractsWithIO::withProgressBar() (4e52a60, 169fd2b)
  • Added Illuminate\Console\Concerns\CallsCommands::callSilently() as alias for callSilent() (7f3101b, 0294433)
  • Added option to release unique job locks before processing (#35255, b53f13e)
  • Added ably broadcaster (e0f3f8e, 6381aa9)
  • Added ability to define table name as default morph type (#35257)
  • Allow overriding the MySQL server version for database queue driver (#35263)
  • Added Illuminate\Foundation\Testing\Wormhole::back() (#35261)
  • Support delaying notifications per channel (#35273)
  • Allow sorting on multiple criteria (#35277, 53eb307)
  • Added Illuminate/Database/Console/DbCommand.php command (#35304, b559b3e)
  • Added Collections splitIn methods (#35295)

Fixed

  • Fixed rendering of notifications with config custom theme (325a335)
  • Fixing BroadcastException message in PusherBroadcaster@broadcast (#35290)
  • Fixed generic DetectsLostConnection string (#35323)
  • Fixed SQL Server command generation (#35317)
  • Fixed route model binding on cached closure routes (eb3e262)

Changed

  • Disable CSRF on broadcast route (acb4b77)
  • Easily set a null cache driver (#35262)
  • Updated aws/aws-sdk-php suggest to ^3.155 (#35267)
  • Ensure ShouldBeUniqueUntilProcessing job lock is released once (#35270)
  • Rename qualifyColumn to qualifyPivotColumn in BelongsToMany & MorphToMany (#35276)
  • Check if AsPivot trait is used instead of Pivot Model in Illuminate\Database\Eloquent\Relations\BelongsToMany (#35271)
  • Avoid no-op database query in Model::destroy() with empty ids (#35294)
  • Use –no-owner and –no-acl with pg_restore (#35309)

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.