Laravel 5.8 is Live – here’s what’s new

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 article, we will take a look at the new features introduced in this new release as listed below:

  • Eloquent “HasOneThrough” relationships
  • Multiple broadcast authentication guards
  • Improved email validation
  • Artisan command improvements
  • Higher order “orWhere” eloquent method
  • Blade file mapping
  • Automatic policy discovery
  • PSR-16 cache compliance
  • Token guard token hashing
  • Default timezone scheduler
  • Mock testing helper methods
  • Dotenv 3.0
  • Added support for new libraries ( Carbon 2.0, Pheanstalk 4.0, Postmark)

Improved email validation

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ö will pass as valid, because it now has support for international characters.

Eloquent relationships

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.

Resource key preservation
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.


namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JSonResource;

class User extends JsonResource {

  public $preserveKeys = true;


Higher order orWhere method
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:


$users = App\User::popular()->orWhere(function (Builder $query) {


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(); 

HasOneThrough relationship
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:


class supplier extends Model {

  public function userHistory(){
      return $this->hasOneThrough('App\History', 'App\User');


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.

PSR-16 cache compliance

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.

Blade file mapping

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.

Artisan command improvements

There are two major improvements to the Laravel CLI (artisan), they are: the artisan call and the artisan serve commands.

Artisan serve
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.

Artisan call
In previous Laravel versions, the artisan commands are passed as an array in the second argument to the artisan call method like so:

Artisan::call('email:send', ['user' => ''];

Laravel 5.8 makes it possible to pass the entire command as the first argument like so:

Artisan::call('email:send --user=');

Automatic policy discovery

In Laravel version 5.7 and older, model policies were required to be explicitly registered in the AuthServiceProvider like so:


protected $policies = [
  'App\Comment' => 'App\Policies\CommentPolicy',
  'App\Users' => 'App\Policies\UserPolicy'


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:

// App\Providers\AuthServiceProvider
use Illuminate\Support\Facades\Gate;


public function boot() {
  Gate::guessPolicyNamesUsing(function ($modelClass) {
    // return policy name

Multiple broadcast authentication guards

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:

use App\Broadcasting\channel;


Broadcast::channel('channel', function(){
  // broadcast message
}, ['guards' =>['web','admin']]);


Token guard token hashing

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:


'api' => [
  'driver' => 'token',
  'providers' => 'users',
  'hash' => true,


Default timezone scheduler

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:

// app\Console\Kernel.php


protected function scheduleTimezone(){
    return 'America/Chicago';


Mock testing helper methods

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:

// Laravel 5.7 
$this->instance(Service::class, Mockery::mock(Service::class, function ($mock) {

//Laravel 5.8 mock()
$this->mock(Service::class, function ($mock) { 

//Laravel 5.8 spy()
$this->spy(Service::class, function ($spy) {

Dotenv 3.0

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:

DEVELOPMENT_APP_KEY="This string extends 
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 check it out onGitHub page.

Added Support for Libraries

Laravel 5.8 comes with added support for some libraries such as carbon, pheanstalk and DynamoDB.

Carbon 2.0 support
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 driver
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 4.0 support 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 article, 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.

This post was originally posted on Pusher.