Yesterday, Taylor Otwell announced Laravel 5.6 just before the Laracon webinar. In Vienna, we attended a small conference and enjoyed watching all the presentations together. During one of the talks, Taylor gave us a tour of Laravel’s new features, and I’ll list three of my favorites.

1. Collision

Collision is a package made by Nuno Maduro and is now part of Laravel’s development dependency. It is an error-handling framework for command line applications, built on top of the Whoops package. Whoops takes care of the browser’s error-handling logic and provides a nice overview of what happened. Collision does the same thing, but for command line applications.

A simple example

Suppose I’m creating a new Laravel command to delete some old backup data. The exact purpose of this command is not important at this point, it is named BackupFlush and is invoked with the command signal BackupFlush :doit.

<? php namespace App\Console\Commands; use Illuminate\Console\Command; Class BackupFlushCommand extends Command {/** * The name and signal of a console Command. * * @var string */ protected $signature = 'backupflush:doit'; /** * Console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the control line command. * * @return mixed */ public function handle() { $this->doIt(); }}Copy the code

In the processing method of the command, a nonexistent method doIt is called. We will use this statement to fire an error. Let’s call this command and see what happens.

php artisan backupflush:doit
Copy the code

Before using Collision, the output looks like this.

In fact, the error message here is pretty descriptive. Isn’t that enough? This is sufficient for our sample, but this is because we intentionally placed the error and knew it was there. Otherwise, finding the location of the called method can be very difficult. So, this is where Collision comes in handy, and it will give you a nice color output.

The response value contains more information about the exception thrown and will help us debug more easily.

2. Dynamic access control

In Laravel, you can limit the frequency of requests for certain routes. Here is an example from the document.

Route: : middleware (' auth: API ', 'throttle: 60, 1') - > group (function () {Route: : get ('/user ', function () {/ /}); });Copy the code

In the example above, the user can only request 60 times per minute for routes under this group. This awesome capability is implemented with throttle middleware. However, in some cases this control is not sufficient, especially if you want to control the frequency through other conditions. This is where dynamic access control comes in. Here is another example code from the documentation:

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

Again, throttle middleware is used, with the frequency set to 1. However, the actual request count does not depend on the Settings here, but rather passes a property name. In this example, rate_limit is a field in your user table that is used to calculate the maximum request count. In this way, different request frequencies can be set for different users. For example, you need to assign different frequencies to users with different subscription plans.

Eloquent date converter

Starting with Laravel 5.6 you can convert the Eloquent property. A common example is a field of Boolean type that stores zeros and ones in the database, but needs to be converted to True or false in the application. This is done by setting the converter in the model.

<? php namespace App; use Illuminate\Database\Eloquent\Model; Class User extends Model {/** * this property is cast as a native Boolean ** @var array */ protected $casts = ['is_admin' => 'Boolean ',]; }Copy the code

Note: See the documentation for all attribute type conversions.

Laravel 5.6 can now convert date types as well:

protected $casts = [
    'is_admin' => 'boolean',
    'released' => 'date:Y',
];
Copy the code

First define the fields that need to be converted as attribute names. Then you define the date function (date or datetime), followed by the date format after the semicolon. In my case, I want to switch to showing only years, so my converter definition is Date :Y.

In the past, model modifiers could do something similar, but they will be replaced by date converters. Therefore, it is recommended that you start using the solution to implement this kind of functionality now, because it is very powerful and practical.

conclusion

In fact, the 5.6 release changes are relatively minor compared to the other releases. Still, it brings a lot of nice features, and I hope you’re as excited as I am about the three features mentioned here. Many thanks to Taylor and all the contributors for their support. Let me know your favorite feature via Twitter.

For discussion please visit Laravel Knowledge Community:
There are three features I like best about Laravel 5.6