By Alvie Amar | 3/7/2017 | General |Beginners

What’s new in Laravel 5.4

What’s new in Laravel 5.4

Last September, Laravel 5.3 was released with its new features and updates, such as Blade’s foreach, and the loop object. And now, just on January 25, 2017 Laravel 5.4 was released and is the current stable version.

 

Sure enough Laravel 5.4 comes with some new and exciting features, so without further ado let’s dive in and see what’s waiting for us this time around!

 

New Features of Laravel 5.4

  1. Two new middlewares
  2. Higher order collection messages
  3. From Laravel Elixir to Mix
  4. Fluent Routing
  5. Blade components and slots
  6. Real Time Facades
  7. Browser Testing with Laravel Dusk
  8. Resourceful Controller with Model Boilerplate
  9. JSON Based Language Files
  10. Markdown Mailables
  11. Map Eloquent Events to Dedicated Classes



# New Middlewares

Out of the box Laravel includes two new middlewares. This middlewares are: TRIM STRINGS MIDDLEWARE and CONVERT EMPTY STRINGS TO NULL.

 Trim strings middleware

  •  This middleware automatically trims extra whitespace from a request data.
  • Located in App/Kernel.php

\Illuminate\Foundation\Http\Middleware\TrimStrings::class

 

Example: In an email input field the user submits something like this:

 email@example.com            "  


The above input will be converted into this: 

 email@example.com"  

Convert Empty Strings to Null

  •  This middleware automatically converts empty request fields to null.

 

Example: The user inputs '  ' an empty string in one of the input fields then the value will be converted to null.



# Higher Order Collection Messages

This just simply means “collection shortcuts”

 

Example:

There’s a collection of articles and we wanted to perform an operation on each item in the collection.

 

Most likely we do it like this:

 

// Loop through all the articles and schedule them to be posted in any social media sites
$articles->each(function ($article) {
   return $article->schedule(ANY_SOCIAL_MEDIA);
});

With the new feature of Laravel, Higher Order Collection Messages. We could simply do it like this: 

$articles->each->schedule(ANY_SOCIAL_MEDIA);

What if we don’t want to post old articles? Then normally we would do it like this: 

/**
* Loop through all the articles and reject any old post
* For the remaining articles, schedule each of them to posted in any social media sites.
*/
$articles>reject(function ($article) {
   return $article->old;
})->each(function ($article) {
   return $article->schedule(ANY_SOCIAL_MEDIA);
});

With higher order collection messages we could simplify the above code with this: 

$articles->reject->old->each->schedule(ANY_SOCIAL_MEDIA);

Pretty cool! 

 

# From Laravel Elixir to Mix

Instead of gulp, the foundation of Laravel Elixir will be built in webpack thus changing the plugin ecosystem, because of this change the package will be renamed to Mix. But both versions will still continue to exist.

Laravel Elixir elixir() helper function was replaced to mix().

 

# Fluent Routing

Normally we create route like this: 

Route::get('article/{id}/edit, function ($id) {
   //
})->name(edit);

With Fluent Routing, we could simply do it like this:  

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

Register a route name and middleware: 

 

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

Middleware with a route prefix and group: 

 

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

Middleware to a Resource Controller: 

Route::middleware('auth')->resource('user', 'UserController');

# Blade Components and Slots

Laravel 5.4 offers a new Blade directive, @component with @slot, which allows HTML elements to be reusable throughout the application.

 

Example: Inject “title” in the alert component 

@component('alert')

    @slot('title')

        Forbidden

    @endslot

   

    Page not Found!


@endcomponent

 

Named slots can be displayed by echoing the variable that matches their name. Any content within the @slot directive will be passed to the component and stored in the $slot variable.

<!-- /resources/views/alert.blade.php --> 

<div class="alert alert-danger">

    <div class="alert-title">{{ $title }}</div> 


    {{ $slot }}

</div>

# Real Time Facades

It is no surprise that Laravel offers facades, which provide a static interface to objects in the service container. In the past releases, in order to create our own facade. We still need to manually construct the relevant facade. But not anymore, in Laravel 5.4 we can make facade on the fly. We just need a namespace class on the facade namespace and we can finally use the class as a facade.

 

Example:

namespace Facade\App\Role;

This automatically converts Role model into a facade. 

 

# Browser Testing with Laravel Dusk

 Laravel Dusk provides easy-to-use browser automation and API testing. Dusk uses a standalone ChromeDriver installation so it does not require to install any JDK or Selenium on your machine. However it could still be utilize with any other compatible selenium drivers.

Basically Laravel dusks run our tests in the browser .

 

Here is an overview of how to make use of Laravel dusk:

  • Add laravel dusk composer dependency: composer require laravel/dusk
  • Register the Laravel\Dusk\DuskServiceProvider service provider wihtin the register method of the AppServiceProvider
  • Run dusk install: php artisan dusk:install
  • Set APP_URL environment in .env file
  • To run test: php artisan dusk
  • To create test: php artisan dusk:make $test_title



# Resourceful Controller with Model Boilerplate

 

A new --model flag is added  when creating a Controller in the command line.

Example: 

php artisan make:controller PostController --model="Post"

Laravel automatically generates a resourceful controller that includes a boilerplate to inject the  Post model in the controller's action. 

# JSON Based Language Files

Instead of PHP files, translation for text in laravel can now be done in JSON files.

 

# Markdown Mailables

We can now build Mailables using the laravel markdown syntax.

 

# Map Eloquent Events to Dedicated Classes

This method cleans up the code significantly. To further understand its use you can watch this laracasts video, https://laracasts.com/series/whats-new-in-laravel-5-4/episodes/10.

 

With all this new features, Laravel  includes a fix in the Laravel 5.4 release. The default database character set was changed to utf8mb4 that includes the supporting of emojis. Applications running on MariaDB and older versions of Mysql may encounter an  error when running migrations.

Error: 

[Illuminate\Database\QueryException]
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))

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

You could fix this by editing the AppServiceProvider.php file and setting the default string length. 

 

use Illuminate\Support\Facades\Schema;

public function boot()
{
   Schema::defaultStringLength(100);
}

That’s all for now. If you missed what’s new in Laravel 5.3 you can check that out, or 

Visit our homepage to search for the coolest PHP development tools.

By Alvie Amar | 3/7/2017 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now