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
- Two new middlewares
- Higher order collection messages
- From Laravel Elixir to Mix
- Fluent Routing
- Blade components and slots
- Real Time Facades
- Browser Testing with Laravel Dusk
- Resourceful Controller with Model Boilerplate
- JSON Based Language Files
- Markdown Mailables
- 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:
|
# 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.
Recent Stories
Top DiscoverSDK Experts
Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
{{compareToolModel.Error}}
{{CommentsModel.TotalCount}} Comments
Your Comment