We're live-coding on Twitch! Join us!
What's New in Laravel 5.4

What's New in Laravel 5.4

If you've been following Scotch for a while, you heard a lot about Laravel. Some months ago, Laravel 5.3 was released. It introduced new features like Mailables.

Yesterday, the 25th of January, Laravel 5.4 was released.

It comes with no surprise that it brought a lot of new and exciting features to Laravel, and for the remainder of this article, we shall go through the new features.

New Middlewares

With the release of 5.4, two new middlewares are now shipped with the framework. If you don't know what middlewares are, check out Understanding Laravel Middlewares.

The middlewares are:

Trim Strings Middleware

Just as the name implies, this middleware trims extra spaces from request data. For example, a user submits their email through a form on your website and mistakenly types in some extra space after the email.

This middleware will automatically trim whitespace, so if a user submits something like this.

// '[email protected]     '

Because of the existence of this middleware (\Illuminate\Foundation\Http\Middleware\TrimStrings::class) in App/Kernel.php, it gets converted into.

// '[email protected]'

Convert Empty Strings to Null

Still, as the name implies, this middleware converts empty strings to null. So if a user submits an empty form instead of getting '' it gets converted to null.

better.dev Get Started w/ JavaScript for free!

Higher order messages

This is perhaps one of my favourite feature ever added to Laravel. It much easier to show than explain.

Imagine we have a collection of blog posts and we want to perform an operation on each item in the collection. Normally, we would do this.

Learn about Collections in Laravel.

// Loop through all the posts and schedule them for sharing to twitter
$posts->each(function ($post) {
    return $post->schedule(TWITTER);

With High order messages in Laravel, we can simplify the above code into this.


Yes, this is now a possibility. It doesn't just stop here either, we can take this a bit further and chain them. Here, another scenario.

* Loop through all the posts and reject any archived post
* Then for the remaining posts, schedule each of them to twitter.
$posts->reject(function ($post) {
    return $post->archived;
})->each(function ($post) {
    return $post->schedule(TWITTER);

With high order messages, we can simplify the above code into this.


Want to see how it's done, check out this commit. This is simply mindblowing

From Elixir to Mix

Laravel Elixir provided us with a gulp wrapper that made development and packaging of assets easier.

The next version of Laravel Elixir is changing the underlying system and will be built on Webpack, instead of Gulp. This will replace the plugin ecosystem, and because of such a significant change, it was time to rename the package.

Also, Laravel Mix comes with a new helper function called mix() that serves as a replacement to the former elixir() function.

If you have a project still using Elixir, you can continue using it, as it is still supported and not going anywhere anytime soon.

Fluent Routing

The Route facade in Laravel is now fluent. Previously, if we wanted to name a route, we would do this.

Route::get('user/{id}/profile', function ($id) {

Now, we can do this.

Route::name('profile')->get('user/{id}/profile', function ($id) {
    // some closure action...

We could register a route name and a middleware

Route::name('users.index')->middleware('auth')->get('users', function () {
    // some closure action...

Registering a middleware with a route prefix and group

Route::middleware('auth')->prefix('api')->group(function () {
    // register some routes...

Registering a middleware to a resource controller

Route::middleware('auth')->resource('photo', 'PhotoController');

Components and Slots

Components and slots allow us to simplify HTML elements into reusable areas.

In our application, we usually have components like modals, notification panel etc. We can now define them as components and reuse them.

To create a component, we simply create a new blade file and refer to it using.

    Modal text go in here

You can read all about components and slots here.

Real-Time Facades

Facades provide a “static” interface to classes that are available in the application’s service container. Laravel ships with many facades which provide access to almost all of Laravel’s features. Laravel facades serve as “static proxies” to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.

One example that comes to mind is Auth. In laravel, we access an authenticated user's data by doing Auth::user(). This Auth class is a facade.

Now in Laravel 5.4, we can create facades on the fly. All we need do is namespace class on the Facade namespace and we can use the class as a facade.

namespace Facade\App\User;

Doing this will automatically convert our User model into a facade.

Laravel dusk

Laravel Dusk provides an expressive, easy-to-use browser automation and testing API. By default, Dusk does not require you to install JDK or Selenium on your machine. Instead, Dusk uses a standalone ChromeDriver installation. However, you are free to utilise any other Selenium compatible driver you wish.

Basically, it means we can test our Laravel code like we would in a web browser. Currently, Laravel uses browser-kit for testing.

The problem with browser-kit is that it is limited, and Dusk serves as an alternative with more features.

Dusk can detect AJAX etc. A full-length article on Dusk is on the way, stay tuned.

Fix: Specified key was too long error

Laravel 5.4 made a change to the default database character set, and it’s now utf8mb4 which includes support for storing emojis. This only affects new applications and as long as you are running MySQL v5.7.7 and higher you do not need to do anything.

For those running MariaDB or older versions of MySQL you may hit this error when trying to run migrations:

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Support\Facades\Schema;

public function boot()


Some features not explained above include

  • Resourceful Controller with Model Boilerplate: in simple terms, it is route model bindings for controllers.
  • JSON Based Language Files: this means that translation for text in Laravel can now be done with JSON files instead of PHP files. Learn More.
  • Markdown Mailables: this allows us to build Mailables in laravel using the Markdown syntax.
  • Map Eloquent Events to Dedicated Classes. Here is a video that further explains this.

If something was omitted, please let us know in the comment. Also, there is a free video series on Laracasts that shows What's New in Laravel 5.4.

Like this article? Follow @KayandraJT on Twitter