Start learning today

Choose Your Plan

Build real life apps. Learn by creating.

Pay Monthly


Pay Yearly


What's New in Laravel 5.4?

Laravel is an amazing PHP framework I am sure we all mostly agree on this. It was no surprise that when Taylor announced the release of Laravel 5.4, we were all stoked. So what is new and how can we leverage the new features in our new applications?

This article will cover some of the newest features of Laravel 5.4, and focus on showing real-life examples of how this can be useful.

Sanitizing Input using Laravel 5.4 Middlewares

While building real life applications, you will always want to sanitize the input that your users feed your application. For example, in the case of a Registration form, it is easy for a user to specify " Neo Ighodaro" (note the leading spaces) as his full name, and by default, it will store to the database with all that space.

With Laravel 5.4 you can now create a sanitizing middleware that will sanitize the data before passing it down to your controller to handle the request. By default, Laravel 5.4 comes with two middlewares registered in app/Http/Kernel.php:

// Trim strings by removing excess spaces from the text

// Converts empty strings to null

These two middlewares basically sanitizes the input before passing it down to the request handler, which in most cases is the controller. You can create your own sanitizing middlewares if you want. You can also attach it to single routes like every other middleware, it does not have to be a global middleware.

Fluent Route definitions

In prior Laravel versions, defining routes could get pretty ugly at times. For instance, in prior versions to Laravel 5.4, we would have to define complex routes like this:

Route::group(['prefix' => 'auth', 'middleware' => 'guest'], function () {
    Route::get('/login', function () {
        return 'Login page';

But in Laravel 5.4, we can now define the routes in a more fluent manner:

Route::prefix('auth')->middleware('auth')->group(function () {
    Route::name('login')->get('login', function () {
        return 'Login Page';

Nifty eh?!

Resource controllers with automatic model binding

That's a mouthful for such a simple feature. So what this means is, with Laravel 5.4 you can now generate controllers with automatically bound models. Let me show you what I mean. Let us assume we have a book table and we want to create a book

$ php artisan make:controller BooksController --model=Book

This will create a new resource controller app/Http/Controller/PostController.php and in the places where appropriate, it will inject the model object. What's more, if the Book model does not exist, it will offer to create it for us.

Taking a good look at the BooksController that was generated, you can see in the show, update and delete methods, the Book model was passed. Here is the edit method of the generated controller. Notice there are no $id variables anymore?

     * Show the form for editing the specified resource.
     * @param  \App\Book  $book
     * @return \Illuminate\Http\Response
    public function edit(Book $book)
        // $book is the equivalent of Book::findOrFail($id)

Then you can go ahead and define your route like this (note the name of the paramenter matching the variable passed to the edit method:

Route::get('books/{book}/edit', 'BooksController@edit');

Higher Order Messaging for Collections

Laravel Collections are already a very powerful tool, but it gets even better with Laravel 5.4. To showcase some of the new things Collections can now do, I will show some examples.

Here are some code comparisons, because I feel showing examples will speak louder than text:

// Laravel <= 5.3
$invoices->each(function($invoice) {

// Laravel >= 5.4

Here's another complex example of Laravel Collections simplifying how you write code:

// Laravel <= 5.3
$employees->reject(function($employee) {
    return $employee->retired; 

// Laravel >= 5.4

That's really crazy! There are certain collection methods that this works for though, so go through the documentation for this.

Blade Components and Slots

Laravel Blade also comes with some new tricks: $slot. This gives us the ability to create reusable components where we can quickly inject content into the components. Let us see an exmple on how this works:

Let us create a blade layout layout.blade.php

<!DOCTYPE html>
<html lang="en">

So now we have a home.blade.php where we add a @component directive


    <h1>Home Page</h1>
        This is an alert.

Now we create a new file alert.blade.php:

<div class="alert">
    {{ $slot }}

Facades on the fly!

Remember Facades in Laravel? Very controversial topic. Well, it's about to get more controversial with real-time Facades. Let's see how this would work.

We should create a new class app/Publisher/Slack.php


namespace App\Publisher;

class Slack {

     * Publishes a message to Slack.
    public function publish($message) {
        echo "Publishing to slack: ".$message;

Now nor mally, to use this class we would have to $slack = new App\Publisher\Slack and then call $slack->publish('Some message') but with real-time facades, you can just do this:

use Facades\App\Publisher\Slack;

Slack::publish('Woot! Woot!');

Awesome! That was easy.

There are many other things that would be too long to post in this article but you can check them out when you have the time.

Neo Ighodaro

CTO at | Founder at, Designing for Web and Mobile for over 12 years.