What’s New in Laravel 5.5
Laravel 5.5 finally released this August. Laravel 5.5 is the most awaited version as it is an LTS (Long term support) release after Laravel 5.1. For LTS release bug fixes are provided for 2 years and security fixes for 3 years.
Laravel 5.5 will need a minimum of php7.0+, so if you haven’t yet upgraded to PHP 7 its right time to do it.
Here I have created a detailed list of new features coming in Laravel 5.5. The list keeps on increasing as we read about important new features.
Whoops Error handler:
It was present in the previous version of Laravel but was removed. With Laravel 5.5 it is coming back. It provides a pretty error interface that helps developers like you and me debug errors easily.
Package link: https://github.com/filp/whoops
Streamlined Request Validation:
CSRF error:
When submitting a form if there is no CSRF token or CSRF token is expired it will show a meaningful error: “The page has been expired due to inactivity. Please refresh and try again”
Better approach for Validate:
Previously when you need to validate a request in controllers you need to write it like:
1 2 3 4 |
$this->validate(request(), [ 'title' => 'required', 'body' => 'required', ]); |
But with Laravel 5.5 provides a better object centric approach:
1 2 3 4 |
$request->validate([ 'title' => 'required', 'body' => 'required', ]); |
Validate returns the validated data:
The validate method above will also return the validated data. So, no need to add the extra statement to get input data after it is validated.
1 2 3 4 5 6 7 8 |
$input = $request->validate([ 'title' => 'required', 'body' => 'required', ]); If title = "The Web Fosters" body = "Laravel based development" print_r($input) = array('title' => 'The Web Fosters', 'body' => 'Laravel based development') |
Note: Validate will return value for the data only which is passed for validation. If you haven’t passed some field then you will have to use request object to get it.
Migration:
Migrate Fresh:
Delete all tables and migrate them again. There is a similar confusing command Migrate re-fresh. The different is re-fresh will first roll back all migrations and then migrate them. But Fresh will simply deletes/drops all table (no rollback here) and migrate all migrations.
This is really helpful indeed.
Frontend Presets:
Present provides quick scaffolding for different javascript framework. In laravel 5.4 it had a default scaffolding using bootstrap and vue. But in laravel 5.5 you can use artisan command and generate scaffolding.
You can either choose none or vue or bootstrap or react and it will create a quick scaffolding to get started.
1 |
php artisan preset type (type = none/vue/bootstrap/react) |
Automatic Package Discovery:
Everytime you install a package, you will have to go to app/config.php and add the provider, aliases etc. It needs to be done for every package we install. But in laravel 5.5 it will be automatically discovered by reading the composer.json files. No more changing the config.php file after installing a new package.
Faster Email Layout Testing
Previously when you need to implement Mailable class(E-mail), we need to send out the email multiple times just to check if the layout looks good. But with Laravel 5.5 you can render email directly in a browser. You will need to return the object of the mail class created and it will get rendered.
So if our class is Welcome, we can render it in a browser by return new App\Mail\Welcome; Technically the Mailable class implements
Technically the Mailable class implements the Renderable interface.
Custom Validation Rules:
New artisan command to create custom validation is added. To add a new custom validation run:
1 |
php artisan make:rule <nameOfClass> |
It will create a Rules directory and the class inside it.
You will have to implement the validation logic and the error message. For this 2 method passes and message needs to be implemented.
1 2 3 4 5 6 7 8 9 10 |
public function passes($attribute, $value) { //Your logic here //Should return boolean true or false. } public function message() { return "This is an Error Message" } |
To use it for validation:
1 2 3 4 5 6 |
$input = $request->validate([ 'title' => ['required', new App\Rules\nameOfClass ], 'body' => 'required', ]); |
Collection Dumping:
Previously when you want to dump a collection you needed to dd or var_dump, like below example:
1 2 3 4 5 6 |
$blogs = App\Blog::all(); $blogs = $blogs ->shuffle() ->filter()->archived() ->pluck('title'); |
Now if want to check the blogs collection after each chained method, we need to comment them and dd it.
1 2 3 |
$blogs = $blogs->shuffle() dd($blogs) |
and so on for other methods as well.
But with Laravel 5.5 there is a new method dump(), with this you can simply add it to the chain after each method and it will show separate output for each of them.
1 2 3 4 5 6 7 8 9 |
$blogs = $blogs ->shuffle() ->dump() ->filter()->archived() ->dump() ->pluck('title') ->dump(); It will output 3 collection, Try it and check the output. |
Model Factor Generator:
In previous versions of Laravel there we a single ModelFactory File, where all factories need to be added. This may get huge with time.
With Laravel 5.5 you can define different factories for different Model.
1 |
php artisan make:factory Blog |
This will create a BlogFactory file where we can define the factory for that Model.
Factories help to create/generate dummy test data set, which can be used for testing purposes.
Custom blade “if” Directives
This is a cool feature added in Laravel 5.5.
If we need to have the same “if” logic in multiple views/places, we can define a custom directive for it quickly and easily.
1 2 3 4 |
@if($age > 18) {{"You are young"}} @else {{"You're still a child"}} |
To define a custom blade directive, go to AppServiceProvder.php and define the directive in boot method:
1 2 3 4 5 |
public function boot(){ \Blade::if('young', function($age){ return $age > 18 }); } |
To use it in view:
1 2 3 4 |
@young($age) {{"You are young"}} @else {{"You're still a child"}} |
1 2 3 4 5 6 7 8 9 10 |
//AppServiceProvder.php - boot method, checing if authenticated or not. \Blade::if('member', function() { return auth()->check(); }) //View, using the 'member' directive created. @member {{"You are a member"}} @else {{"You're Guest"}} |
Auto-register Artisan Command
If you create a new artisan command you will no longer have to switch to kernel.php and register it there. The command will auto register.
API Resources
While building API we need a layer to transform Model data into JSON. Laravel’s resource classes allow you to expressively and easily transform your models and model collections into JSON. It provides a Lot of options to wrap your response they way you want, paginate the result, adding conditional attributes, including relationship data. Check out the documentation to know in details about it.
New Route Helpers
Redirect Helper
If you want to redirect a URI to other URI the standard approach will be
1 2 3 |
Route::get('/home', function(){ redirect('/dashboard') }) |
but with laravel 5.5 it can be done in more cleaner approach
1 |
Route::redirect('/home', '/dashboard'); |
Route View Helper
Traditional approach to rendering a view for a URI is
1 2 3 |
Route::get('/', function(){ return view('home') }) |
But with Laravel 5.5 it can be done in more cleaner approach
1 |
Route::view('/', 'home'); |
******
That’s all we have for now.
Did we miss something important? Any other suggestion? We would love to hear back from you.
No Comments