Laravel 5.8 was released on February, 26th 2019. This new release continues the improvements made in the previous release (version 5.7), as well as new features which includes support for the postmark email service, “has-one-through” Eloquent relationships, improved email validation, and many others.
In the course of this tutorial, we will take a look at the new features introduced in this new release as listed below:
As an improvement to the Laravel email validator, a package used by SwiftMailer was utilized. Thanks to a package by egulias/email-validator , email validation logic in Laravel 5.8 has been improved. As a result of this improvement, email addresses such as hello@yölo.com will pass as valid, because it now has support for international characters.
Laravel 5.8 comes with a few improvements such as resource key preservation, higher order orWhere
method and HasOnethrough
relationships. In this section, we will take a look at these new features.
In Laravel 5.7, when returning a resource collection, Laravel resets the collection keys so they are in numerical order. Laravel 5.8 makes it possible to preserve collection keys by setting the preservedKeys
property in the App\Http\Resources
namespace to true
when this is done, the collection keys will be preserved.
1//App\Http\Resources 2 3 namespace App\Http\Resources; 4 5 use Illuminate\Http\Resources\Json\JSonResource; 6 7 class User extends JsonResource { 8 9 public $preserveKeys = true; 10 11 }
In previous versions of Laravel, using the orWhere
high order function (a function that accepts a function as an argument) required a closure callback like so:
1... 2 3 $users = App\User::popular()->orWhere(function (Builder $query) { 4 $query->active(); 5 })->get(); 6 7 ...
In version 5.8, Laravel makes it possible to chain all these local scopes together without the use of a closure callback, Hence a “higher” high order orWhere
method like so:
$users = App\User::popular()->orWhere->active()->get();
The eloquent HasOneThrough
relationship was an issue raised by Dwight Watson on the 13th of May 2015 and was finally worked on by Dries Vints. This relationship makes it possible to link models using a single intermediate relation or a middle man.
Suppose we have three tables: users
, suppliers
, and history
. Let us assume that the suppliers
table does not explicitly relate to the history
table. The users
table can be used as an intermediary to provide the supplier
table access to the history
table like so:
1... 2 3 class supplier extends Model { 4 5 public function userHistory(){ 6 return $this->hasOneThrough('App\History', 'App\User'); 7 } 8 9 ...
Where App\History
is the model we want to access and App\User
the pivot model. To know more about the HasOneThrough
relationship you can check out the Laravel documentation.
In adherence to the PSR-16 caching standard, several methods in the Illuminate\Cache\Repository
class, which includes put
, add
and many others, have been updated to change the TTL (time-to-live) of each cached item from minutes to seconds. Where ambiguity arises when converting minutes to seconds, a DateTime
instance can be passed instead like so:
Cache::put('foo', 'bar', now()->addSeconds(30));
This method makes it easy to upgrade existing applications to Laravel 5.8 instead of explicitly converting every instance of cache time in minutes
to seconds
.
In Laravel 5.8, Laravel places a comment at the top of every Blade file, which provides a path to the original Blade template file. This is feature helps Blade debugging in the PhpStorm IDE by JetBrains.
There are two major improvements to the Laravel CLI (artisan), they are: the artisan call
and the artisan serve
commands.
Version 5.8 brings some improvement to Laravel’s CLI tool (Artisan). In the previous versions of Laravel, the php artisan serve
command could only serve on port 8000 by default. This new feature makes it possible to serve more than one Laravel applications at once, by utilizing all available ports between 8000 - 8009.
In previous Laravel versions, the artisan commands are passed as an array in the second argument to the artisan call method like so:
1... 2 Artisan::call('email:send', ['user' => 'foo@bar.com']; 3 ...
Laravel 5.8 makes it possible to pass the entire command as the first argument like so:
1... 2 Artisan::call('email:send --user= foo@bar.com'); 3 ...
In Laravel version 5.7 and older, model policies were required to be explicitly registered in the AuthServiceProvider
like so:
1... 2 3 protected $policies = [ 4 'App\Comment' => 'App\Policies\CommentPolicy', 5 'App\Users' => 'App\Policies\UserPolicy' 6 ]; 7 8 ...
This had to be done for every policy created in the application. With Laravel 5.8, the need for policy registration was eliminated so long as the naming and location of policies were according to standard.
The standard in the policy name must match the model name and have a “Policy” suffix. The policy should also be located in the app\Policies
directory in the same namespace as its corresponding model. However, to use a different naming convention, we can register a custom callback calling using the Gate::guessPolicyNamesUsing
method in the AuthServiceProvider
like so:
1// App\Providers\AuthServiceProvider 2 use Illuminate\Support\Facades\Gate; 3 4 ... 5 6 public function boot() { 7 Gate::guessPolicyNamesUsing(function ($modelClass) { 8 // return policy name 9 } 10 }
Private channels require that only authorized users currently authenticated can listen in on the channel. In the previous version of Laravel, users are authenticated using the default authentication guard. Laravel 5.8, however, makes it possible to assign multiple guards which will handle the authentication of incoming requests like so:
1use App\Broadcasting\channel; 2 3 ... 4 5 Broadcast::channel('channel', function(){ 6 // broadcast message 7 }, ['guards' =>['web','admin']]); 8 9 ...
Laravel’s API authentication guard now supports secure hash algorithm SHA - 256 to be used for basic API authentication. This feature can be used setting up the api
guard in the config/auth.php
like so:
1... 2 3 'api' => [ 4 'driver' => 'token', 5 'providers' => 'users', 6 'hash' => true, 7 ], 8 9 ...
In previous versions, one could specify what time and timezone a task or cron job will run. However, specifying timezones for each and every task in the application became a bit of a hassle. In the spirit of DRY (don’t repeat yourself), Laravel 5.8 makes it possible to define the scheduleTimezone
method, which will return the default timezone for all scheduled tasks like so:
1// app\Console\Kernel.php 2 3 ... 4 5 protected function scheduleTimezone(){ 6 return 'America/Chicago'; 7 } 8 9 ...
Laravel 5.8 added two new methods mock
and spy
. These two methods will make the process of mocking objects simpler. These methods automatically inject the mocked class into the service container like so:
1// Laravel 5.7 2 $this->instance(Service::class, Mockery::mock(Service::class, function ($mock) { 3 $mock->shouldRecieve('process')->once(); 4 } 5 6 //Laravel 5.8 mock() 7 $this->mock(Service::class, function ($mock) { 8 $mock->shouldRecieve('process')->once(); 9 ... 10 } 11 12 //Laravel 5.8 spy() 13 $this->spy(Service::class, function ($spy) { 14 $spy->shouldHaveRecieved('process')->once(); 15 ... 16 }
Laravel 5.8 ships with support for the version 3.0
of dotenv. Dotenv helps users keep environment variables such as API keys, server login details and any other sensitive information.
Version 3.0 makes it possible to write multiple line .env
variables as opposed to the previous versions like so:
1DEVELOPMENT_APP_KEY="This string extends 2 to a new line"
This will return the string with white spaces and new line intact as opposed to the version two and one where “to a new line” will be stripped from the string. To know more about dotenv you can checkout its GitHub page.
Laravel 5.8 comes with added support for some libraries such as carbon, pheanstalk and DynamoDB.
The 2.0
release of the Carbon date manipulation library comes out of the box in Laravel 5.8. This removes the extra step of including Carbon in multiple Laravel application individually.
Postmark is a fast and reliable email service used to send transactional mails. Laravel 5.8 makes it possible to utilize the Postmark mail driver as seen below. To check out pricing plans, documentation, and other features of the Postmark app on this link.
Pheanstalk is a pure PHP 7.1+ client for the beanstalkd workqueue. Laravel 5.8 provides support for version 4 of the Pheanstalk queue library. An upgrade to version 4.0
is advised in the Laravel upgrade guide. The latest release can be installed from composer
In this tutorial, we have looked at the new features of Laravel 5.8. To upgrade your current application to version 5.8, you can check out the upgrade guide as well as the release notes.