Neat Laravel Features from 2017

Photo by Mark Golovko

We passed a year again, (oh, Happy New Year, before we forget), so we decided to collect some features from the Laravel framework that added in the previous year.

There were two big version changes, 5.4 in January and 5.5 in August. Of course, the big releases brought huge changes, but we try to focus on those that added during the year, without any bigger announcement.

The tap() method (v5.4.10)

The tap method is a quite well known feature now. The point of this feature to make the change to process a task on the items of the collection, without affecting the collection itself. It can be very useful!


The when() method (v.5.4.12)

As the tap, the when also became a popular feature of the Collection module. With this method, we can modify the collection if the given condition is true.


API routes with the apiResource() method (v5.4.24)

Since, we worked with APIs, we always had to exclude some routes from our route definitions. For example:

Route::resource(‘api/posts’, ‘PostsController’)->except([
‘create’, ‘edit’,

With the api resources, our job is a bit more easier:

Route::apiResource(‘api/posts’, ‘PostsController’);

A bit more cleaner, right?


Guest and Auth blade directives (v5.4.29)

If we wanted to hide / show things in our views, based on the authentication status of the user, we always had to wrap the check in an @if-@else blade directive.

Now we can use the @guest-@endguest and @auth-@endauth directives. You can clean up your views and start using these.


The JSON blade directive (v5.5.3)

When we had to print some JSON in our blade templates, we had to convert them manually or use Collection to cast our data to JSON. Then we had to wrap them in the {{ }} to print the data.

With the @json directive, we can just pass the data and it will take care of the rest.


The -m shortcut to add the proper model for a factory (v5.5.5)

This makes us able, to instantly add the model to the factory, instead of replacing the model manually.

php artisan make:factory ArticleFactory -m Article

It saves time and energy!


The includeFirst blade directive (v5.5.5)

If we want to include templates dynamically, we can replace the if statements with a clean approach.

// Before
// After
@includeFirst(['custom-template', 'default-template'])


The pad() method (v5.5.13)

The pad method fills the array with the given value, until it reaches the specified size.


The firstWhere() method (v5.5.23)

This method returns the first element when the given condition passes. Also, it stops the loop, so it is a bit more performance friendly.



Of course, this list is not complete at all. We just want to highlight some of the features we used frequently.

It’s insane how these little features increases the value of Laravel, by solving small but still real needs.

We strongly suggest to follow the changelog, to be up-to-date of the most recent changes in this year too.