What’s new in Laravel 5.6 – Lravel 5.6 New Features

by jay patel




Laravel 5.6 was released shortly before this year’s Laracon online. This release continues the improvements made in Laravel 5.5 by adding more new features and improvements. In this article, I’ll cover the new features and improvements added to Laravel 5.6.

Let’s start by listing each of them, then we’ll go over them one after the other:


– Bootstrap 4
– Argon2 password hashing
– API controller generation
– Logging improvements
– Single server task scheduling
– Dynamic rate limiting
– Broadcast channel classes
– Model serialization improvements
– Eloquent date casting
– Blade component aliases
– UUID methods
– Collision

Now let’s go over each of them.

Bootstrap 4

Bootstrap 4 has been officially released after over two years of it being in development. In this vein, all frontend scaffolding such as the authentication boilerplate, example Vue component and even the pagination link generation in Laravel have been upgraded to use Bootstrap 4.

If you are upgrading your application from Laravel 5.5, you can continue to use Bootstrap 3 by adding the line of code below to the boot() method of app/Providers/AppServiceProvider:

 

public function boot ()

{

Paginator::useBootstrapThree();

 }

 

 

 

This will allow your application to use Bootstrap 3 as the default styling for pagination links.

Support For Argon Password Hashing

Argon2, the recommended password hashing algorithm by the Password Hashing Competition, is a modern algorithm for securely hashing passwords. And it comes in two distinct flavors, Argon 2i and Argon 2d. PHP 7.2 recently added support for Argon 2i password hashing. Therefore, Michael Lundbol took the initiative to add support for Argon hashing in Laravel.

Argon

Initiative to add Argon Password Hashing

The bcrypt driver is the default driver for password hashing in Laravel. However, Laravel 5.6 now supports argon.

Argon Driver Support

API controller generation

Laravel 5.6 makes it much easier to build APIs with Laravel. It introduces a new --apiswitch to the make:controller Artisan command:

$ php artisan make:controller API/BookController -- api

This will generate BookController with only the Methods (index, store, update, destroy) needed when building APIs. This prevents having create() and edit() littering around or having to manually delete them yourself, since they are not useful when building APIs.

This introduction, combined with the apiResource() while defining resourceful routes, means awesome experience building API with Laravel. 

Logging improvements

Logging in Laravel has become a lot better with the release of Laravel 5.6. The logging system now has it own dedicated configuration file. You can now easily build logging stacks  that send log messages to multiple handlers.

 

'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',

],

],

 

From the config file above, we are stacking multiple channels (syslog  and slack) so that all debugnbsp;level messages will be sent to the system log while all error level messages be sent to Slack so that your team can quickly react to errors.

It is also now easier to customize existing log channels using the logging system’s new tapfunctionality. For more information, check out the docs.

Single server task scheduling

Another addition made to Laravel 5.6 is single server task scheduling, which allows the ability to limit a scheduled job to only execute on a single server. Before Laravel 5.6, If your application is running on multiple servers and the task scheduler is also running on these servers, then the scheduled tasks will run multiple times.

Now you can indicate that the task should run on only one server, by making use of the onOneServer() method:

 

$schedule->command('report:generate')

->weekly()

->onOneServer();

 

To utilize this feature, your application must be using the  memcachedorredis cache driver as your application’s default cache driver. In addition, all servers must be communicating with the same central cache server. For more information, check out the docs.

Better Support For Dynamic Rate Limiting

One of the beautiful built-in features of Laravel is API Rate Limiting. Before now, Laravel's rate limiting configuration involved specifying a hard-coded number of requests on a group of routes. However, Laravel 5.6 adds icing to the cake by allowing you specify a maximum number of requests based on an authenticated User model attribute.

 

Route::middleware

('auth:api','throttle:rate_limit,1')->

group(function () {

Route::get('/users',function () {

// do something awesome

});

});

 

In the code above, the rate_limit parameter is an attribute of the Usermodel in a Laravel 5.6 application.

Broadcast channel classes

Previously when broadcasting on a private channel, we have to define a Closure in the routes/channels.php file which is used to determine whether a user is logged in or not. This can easily become bulky depending on your application. So in Laravel 5.6, instead of using Closures to authorize channels, you may now use channel classes. We can make use of the new make:channel Artisan command to generate broadcast channel classes:

$ php artisan make:channel ChatChannel

This will create a new ChatChannel channel class in the App/Broadcastingdirectory.

Next, we can register the channel in the routes/channels.phpfile:

 

useApp\Broadcasting\ChatChannel;

Broadcast::channel('chat',ChatChannel::class);

 

Then we’ll define the authorization logic in the join() method of the ChatChannel channel class. The join() method will contain the authorization logic that we would have typically placed in your channel authorization closure:

 

namespaceApp\Broadcasting;

useApp\User;

useIlluminate\Support\Facades\Auth;

classChatChannel

{

/**

* Create a new channel instance.

*

* @return void

*/

publicfunction__construct()

{

//

}

/**

* Authenticate the user's access to the channel.

*

* @param \App\User $user

* @return array|bool

*/

publicfunctionjoin(User $user)

{

returnAuth::check();

}

}

For more information, check out the docs.

Model serialization improvements

Prior to Laravel 5.6, queued models would not be restored with their loaded relationships intact. With the improvement to Laravel 5.6, now relationships that were loaded on the model when it was queued are automatically re-loaded when the job is processed by the queue.

Elevated Eloquent Date Formatting

Laravel 5.6 provides a subtle way to cast Eloquent date model attributes to a specific date format. All you need to do is to specify the desired date format in the $casts array.

"Laravel 5.6 provides a subtle way to cast Eloquent date model attributes to a specific date format."

TWEET THIS 

Now, you can customize the date model attributes like so:

 

protected$casts = [

'date_enrolled' =>'date:Y-m-d',

'date_evicted'=> 'datetime:Y-m-d H:00',

];

 

When the model is cast to JSON or array output, the attributes will be formatted to the date formats example provided above like so:

 

date_enrolled: 2005-02-19

date_evicted: 2018-02-20 8:30

 

Blade component aliases

Another new feature in Laravel 5.6 is Blade component aliases. You can now alias your Blade components stored in a sub-directory for easier access. Let’s say, for instance, we have a Blade component that is stored at

resources/views/components/notification.blade.php.

We can use the component method to alias the component from components.notificationto:

 

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

 

Then we can render it using a directive:

 

@notification('notification', ['type' => 'danger'])

Your account has been successfully upgraded!

@endnotification

// we can even omit the component parameters if it has no additional slots

@notification

Your account has been successfully upgraded!

@endnotification

 

For more information, check out the docs.

UUID methods

Laravel 5.6 introduces two new methods for generating UUIDs:Str::uuid and Str::orderedUuid:

 

useIlluminate\Support\Str;

$uuidString=(string)Str::uuid();

$orderedUuidString=(string)Str::orderedUuid();

 

The orderedUuid method will generate a timestamp-first UUID that is more easily and efficiently indexed by databases such as MySQL. Each of these methods returns a Ramsey\Uuid\Uuid.

Collision

Collision Package created and maintained by Nuno Maduro an error handler package for PHP command line applications. This package gives you beautiful error reporting once you interacting with your Laravel applications through command line or CLI.

You will get error reporting in your terminal like this:

laravel 5.6
 

Laravel 5.5 is built on the top of the Whoops error handler and it’s supports artisan and PHPUnit. Please check Collision Github repo for more information about this package.

Miscellaneous

  • Laravel 5.6 requires PHP 7.1.3 and above.

  • The Artisan optimize command was deprecated as of 5.5 and has been removed officially from 5.6.

  • Laravel 5.6 includes two new Blade directives: @csrf and @method.

You can view the complete change logs on GitHub.

Conclusion

Laravel 5.6came loaded with new features and significant improvements.You should check the Laravel release notes for more information and the upgrade guide to see how to upgrade your application to Laravel 5.6.

Have you upgraded to Laravel v5.6 yet? What are your thoughts? Let me know in the comments section! 😊



Leave a Reply

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

   Confirm you are not a spammer
   Notify me of follow-up comments by email.