The new Laravel 5.2 has been out for a month now but it brought a lot of improvements by adding simplified Eloquent global scopes, multiple authentication driver support, opt-in authentication scaffolding, implicit model binding, array validation improvements, middleware groups, rate limiting middleware, and more.
Auth Scaffolding
The auth scaffolding will automatically create a base set of view files for registration, authentication, and password resets. It can be ran with:
php artisan make:auth
Implicit model binding
What implicit model binding does is to automatically bind a model to a route. Here is an example:
Route::get('/api/posts/{post}', function(Post $post) {
return $post;
});
This calls the Post::findOrFail($post)
and injects it into the $post variable. For the experienced Laravel developers, this would be similar to the existing route model binding but it removes the step of binding it manually.
Appending output from scheduled tasks
The Laravel Scheduler is now capable of appending output from the task to a file.
$schedule->command('emails:send')
->hourly()
->appendOutputTo($filePath);
In previous versions, Laravel included a sendOutputTo
option, which wrote the current results but not append.
Form Array Validation
Let’s say that you have a form with an array of input fields:
<p>
<input type="text" name="person[1][id]">
<input type="text" name="person[1][name]">
</p>
<p>
<input type="text" name="person[2][id]">
<input type="text" name="person[2][name]">
</p>
Until now, adding validation rules required looping through and adding the rules individually. Instead, you can achieve all that now like so:
$v = Validator::make($request->all(), [
'person.*.id' => 'exists:users.id',
'person.*.name' => 'required:string',
]);
Database Session Driver
The database session driver now includes ip_address
and user_id
so you can easily clear all sessions for any given user.
Collections Wildcards
When you’re using a collection and want to pull out data, you can now use a *
as a wildcard:
$posts->pluck('posts.*.title');
This will return all titles for all posts.
Middleware Groups
Middleware groups allow you to group several route middleware under a single convenient key, allowing you to assign several middleware to a route at once. This can be useful when building a web UI and an API within the same application. You can group the session and CSRF routes into a web group, and perhaps the rate limiter in the api group.
Rate Limiting
A brand new rate limiter middleware is included with the framework from now on, allowing you to limit the number of requests a given IP address can make to a route over previously specified number of minutes. For example, to limit a route to 30 requests every minute from a single IP address, you can do the following:
Route::get('/api/users', ['middleware' => 'throttle:30,1', function () {
//
}]);
Eloquent Global Scope Improvements
Up until now, global Eloquent scopes in Laravel were complicated and error-prone to implement; however, in Laravel 5.2, global query scopes require you only to implement a single, simple method: apply
.
So that is it, don’t forget to tell us what you think of the improvements in the comments below. Is there something you are particularly not happy with or on the contrary – extremely happy with? I would also love it if you shared the article with people who will it useful.
Laravel, which is otherwise known as PHP on Rails. 😉