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!

Docs: https://laravel.com/docs/5.5/collections#method-tap

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.

Docs: https://laravel.com/docs/5.5/collections#method-when

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?

Docs: https://laravel.com/docs/5.5/controllers#restful-partial-resource-routes

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.

Docs: https://laravel.com/docs/5.5/blade#if-statements

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.

Docs: https://laravel.com/docs/5.5/blade#displaying-data

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!

PR: https://github.com/laravel/framework/pull/21219

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
@if(view()->exists('custom-template'))
@include('custom-template')
@else
@include('default-template')
@endif
// After
@includeFirst(['custom-template', 'default-template'])

Docs: https://laravel.com/docs/5.5/blade#including-sub-views

The pad() method (v5.5.13)

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

Docs: https://laravel.com/docs/5.5/collections#method-pad

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.

Docs: https://laravel.com/docs/5.5/collections#method-first-where

Summary

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.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.