Laravel

What’s new in Laravel 5.6? (A Complete Guide to New Features)

February 28, 2018

author:

What’s new in Laravel 5.6? (A Complete Guide to New Features)

We all know Laravel is a fast advancing framework. Probably that’s what makes it so loved within the community. It keeps developers on their toes to understand and learn new features. And hence its that time of the year where community expects updates or bug fixes in the framework. Taylor Otwell announced officially announced the release of Laravel 5.6 in a Laracon Online Conference and was released on 7th February 2018.

Let us jot down some of the important updates in Laravel 5.6:

# Prerequisites

Laravel 5.5 was an LTS version, and it had following requirements:

  • PHP >= 7.0.0
  • PHPUnit 6
  • Symfony >=3.3

While Laravel 5.6 requires a higher version of them:

  • PHP >= 7.1.3
  • PHPUnit 7
  • Symfony >=4.0

# Task Scheduling on a Single Server

Laravel 5.5, tasks run on every server. Even when an application runs on multiple servers, the scheduled tasks run on all of those servers. This is expected but takes a hell lot of load and may end up duplicating resources.

With Laravel 5.6, this is fixed. It has an option to limit the scheduled jobs to run on a single server. onOneServer() method is introduced which can be used while defined a scheduling task. The server which obtains the task first will add a security layer with an atomic lock on this specific job. This prevents other servers from getting a hold of the task in the same period of time. Here’s how you can define such tasks:

$schedule->command('backup:generate')
    ->tuesdays()
    ->at('4:00')
    ->onOneServer();

But keep in mind that this works only Redis or Memcached cache drivers as they have this feature of atomicity.

# Improved Logging

This is one of the major updates in Laravel 5.6. Most of the chunks are rewritten to use Monolog handlers. It can send log messages to multiple handlers by building logging stacks. Also the logging file has been moved to config/logging.php from config/app.php. We can use the same old Log Façade for logging and exception handling. Here’s how you can combine multiple channels:

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['syslog', 'slack'],
    ],

    'syslog' => [
        'driver' => 'syslog',
        'level' => 'debug',
    ],

    'slack' => [
        'driver' => 'slack',
        'url' => env('LOG_SLACK_WEBHOOK_URL'),
        'username' => 'Laravel Log',
        'emoji' => ':boom:',
        'level' => 'critical',
    ],
],

Here, we are sending all the debug messages to system log while error logs to slack.

# Broadcasting Channel Classes

Laravel 5.5 stores channels within routes/channels.php. When Laravel application has more channels, the channels.php becomes quite cumbersome.

But Laravel 5.6 attempts to solve this. It has a brand new artisan command make:channel:

php artisan make:channel DownloadChannel

With this, we can now use the channel class instead of using closures to authorize any channel with join() method of the channel. And thus we can keep the route registration clean in routes/channels.php file:

use App\Broadcasting\DownloadChannel;

Broadcast::channel('order.{order}', DownloadChannel::class);

 

# Error Reporting with Collision Package

A collision is a PHP error handling package typically for command line applications. In v5.5, it was built on top of Whoops error handler for HTTP exception pages. With v5.6, error packaging for CLA also came to life as pre-installed dev composer dependency. With this, developers get a clean exception, with the code where things went wrong right from their console.

What's new in Laravel 5.6, Collision

 

# Password Hashing with Argon2

Until Laravel 5.5, the passwords were hashed with Bycrypt Algorithm. They still happen to work in v5.6. But now developers have an option to use Argon2 instead. Argon2i is more powerful and consumes more resources of hacker machines, thus proving expensive for them. Hashing driver settings are now available in new config/hashing.php configuration file.

# Eloquent Date Casting

We can now customize the date format in Eloquent Models. The format of Eloquent date and time casting can now be customized individually. This is a breath of fresh air for the applications that require each attribute in a different format:

protected $casts = [
    'sign_up_date' => 'date:Y-m-d',
    'last_activity_at' => 'datetime:Y-m-d H:00',
];

 

# Rate Limiting

With Laravel 5.6, we can now limit on a per-user basis dynamically. It takes flexibility to a new level. For instance,

Route::middleware('auth:api', 'throttle:rate_limit,1')
    ->group(function () {
        Route::get('/user', function () {
            //some code
        });
    });

Here, rate_limit is an attribute of User model which determines the number of requests allowed for a given time limit.

# Bootstrap 4

Laravel finally has Bootstrap 4 out of the box. All the basic front-end pages like welcome, auth boilerplate, Vue component now are created with Bootstrap 4. Also, note that the styles for pagination have been moved to Bootstrap 4 from this new version.

# New Blade Directives

Two new form blades are shipped with Laravel 5.6.

CSRF Blades & spoofed HTML methods:

v5.5:
Since HTML doesn’t allow requests like PUT, PATCH, and DELETE, we had to pass them as hidden input methods:

<form>
    {{ csrf_field() }}
    {{ method_field('PUT') }}
    ...
</form>

v5.6:
But Laravel 5.6 now has a cleaner way to do it:

<form>
    @method('put')
    @csrf
     ...
</form>

 

# Blade Component Aliases

Laravel 5.6 supports Alias Blade Directive registration. For instance, if there’s component stored in resources/views/components/sidebar.blade.php, we can access them:

In Laravel 5.5,

@component('components.sidebar')
    Hello Sidebar...
@endcomponent

But in Laravel 5.6,
We can use Blade::component function in the component:

Blade::component('components.sidebar', 'sidebar');

As the component is aliased, we can render this alias:

@sidebar('sidebar', ['type' => 'left'])
    Hello Sidebar…
@sidebar

 

# Generate API Resource Controller

Resource controllers are very useful. Earlier they were available for HTTP routes. Laravel 5.6 is equipped with a command to generate API Resources. It excludes create and edit methods which are applied to resources returning HTML. To generate such resource, use –api flag:

php artisan make:controller API/CategoryController --api

Parting Thoughts:
No doubt Laravel is one of the fastest pacing web development framework. We just saw several instances to prove that. Laravel 5.6, the latest version has some significant improvements. We also saw the apparent difference from Laravel 5.5 at some places. Hope the overall ideas were clear. So what are you waiting for? Go on, and start Laraveling!

Leave a comment

Your email address will not be published. Required fields are marked *