In this article we are going to see what are we getting from laravel out of the box in its most recent version 5.7.
If you haven’t gone through the latest laravel documentation then you can click this link here or follow this article below.
Let’s get started
laravel have 9 major changes in this current release. We are going to mention them in points and learn more brief throughout the article.
- Laravel Nova
- Email Verification
- Guest User Gate / Policies
- Symfony Dump Server
- Notification Localization
- Console Testing
- URL Generator & Callable Syntax
- Paginator Links
- Filesystem Read / Write Steams
Below we are going to disucss in detail about what are the new changes and release in 5.7
So, What’s New Coming to laravel 5.7 Release ?
Laravel Nova
Laravel nova is the most beautiful and elegantly designed administration panel for laravel. It has code driven configration and can be integrated without touching anything in your eloquent models and making changes in your current laravel application.
Nova is the sleek administration panel which is designed with the help of JavaScript framework Vue. It was previously mentioned by Taylor during the Laracon US 2018 which was back official released in August 22 2018 as Orion but later it changed to nova.
If you want to learn more about it check Laravel Nova official homepage
Email Verification
Finally its here, we know most of the websites asks for email verification after user registration to prevent unwanted signups and verify the real users email. In the past we need to create our own verification system to enable activation link to the email, but now laravel ships it out of the box.
We just need to implement MustVerifyEmail
interface inside the User Model and other are handled it self by laravel. Additionally you can bind ‘verified’ Middleware to the routes protecting only verified users to pass through the URL.
You can read more at laravel email verification activation code tutorial that i wrote earlier to know more about it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements MustVerifyEmail { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; } |
Guest User Gates / Policies
Previously laravel used to return false
automatically for unauthorized users when visitors visits the website. Now,we can allow guest users to pass through authorization checks declaring optional or supplying null
value to the definition.
1 2 3 |
Gate::define('allow-guest', function (?User $user, Post $post) { /* ... */ }); |
Symfony Dump Server
Symfony dump server package is recently introduced by the community member of laravel whose name is Marcel Paciot. It is very useful to debug your application in runtime. You can check this by hitting php artisan dump-server
.
To have a clean demo just add this in your routes/web.php
.
1 2 3 4 |
Route::get('/demo', function(){ $user = User::all(); dd($user); }); |
After activating the dump server just php artisan serve
and hit the demo URL that we just added in the routes.
http://localhost:8000/demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
C:\xampp\htdocs\laratutorials>php artisan dump-server Laravel Var Dump Server ======================= [OK] Server listening on tcp://127.0.0.1:9912 // Quit the server with CONTROL-C. GET http://localhost:8000/demo/ ------------------------------- ------------ --------------------------------- date Sun, 09 Sep 2018 03:09:35 +0000 controller "Closure" source web.php on line 26 file routes\web.php ------------ --------------------------------- Illuminate\Database\Eloquent\Collection {#229 #items: array:1 [ 0 => App\User {#230 #fillable: array:3 [ 0 => "name" 1 => "email" 2 => "password" ] #hidden: array:2 [ 0 => "password" 1 => "remember_token" ] #connection: "mysql" #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:8 [ "id" => 1 "name" => "Shashank Bhattarai" "email" => "Sorry i can show my email :)" "email_verified_at" => null "password" => "$2y$10$CTBQRlXUqpiqLaGa5Y/vquEIclrjBnud6YjiFX3GeDkuxuIpfJLmG" "remember_token" => "M00EIFz30Q7YPPmlywI7VwmO60eQXFXuXBlKgZIs2IL3r5dtJVia2dGGPUgZ" "created_at" => "2018-09-08 14:52:38" "updated_at" => "2018-09-08 14:52:38" ] #original: array:8 [ "id" => 1 "name" => "Shashank Bhattarai" "email" => "Sorry i can show my email :)" "email_verified_at" => null "password" => "$2y$10$CTBQRlXUqpiqLaGa5Y/vquEIclrjBnud6YjiFX3GeDkuxuIpfJLmG" "remember_token" => "M00EIFz30Q7YPPmlywI7VwmO60eQXFXuXBlKgZIs2IL3r5dtJVia2dGGPUgZ" "created_at" => "2018-09-08 14:52:38" "updated_at" => "2018-09-08 14:52:38" ] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #visible: [] #guarded: array:1 [ 0 => "*" ] #rememberTokenName: "remember_token" } ] } |
You can get this dump server data by using php artisan dump-server --format=html > debugReport.html
Notification Localization
We can create notification and can only send in english language. But now laravel allows us to send rather than current language which is default set to English but also different language.
To get this result we need to use Illuminate\Notifications\Notification
class which offers a new method called locale
which can be used to set the desired language.
1 |
$user->notify((new UserActivate($user))->locale('np')); |
We can also utilize the facade to set localization for multiple notifications.
1 2 |
Notification::locale('np') ->send($users, new SendEntries($entries)); |
Console Testing
New feature of laravel allows us to easily mock the data into our console commands. We can do this by using expectedQuestion();
function and specify the exit code that needed to be in output by using assetExitCode();
and expectedOutput();
. We can be more clear by using the example below.
1 2 3 4 5 6 7 8 9 10 11 |
Artisan::command('question', function () { $pos= $this->ask('What is your position'); $framework= $this->choice('Which Framework do you currently use in php?', [ 'Laravel Framework', 'Zend Framework', 'Yii Framework', ]); $this->line('Position : '.$pos.' and you program in '.$framework.'.'); }); |
URL Generator & Callable Syntax
In the past laravel only used to suport strings. But now it also supports callable array syntax. We can demonstrate by using the simple example.
Previously,
1 2 |
$url = action('HomeController@viewHomepage'); $url = action('HomeController@viewSinglePage', ['id' => 1]); |
Now,
1 2 |
$url = action([HomeController::class, 'viewHomepage']); $url = action([HomeController::class, 'viewSinglePage'], ['id' => 1]); |
Paginator Links
In the past we cannot control the number of links inside the paginated URL. By default it was (3) on each site of the paginated links. But laravel recently intriuced the new method that we can use onEachSide()
to control them in this version
1 |
{{ $posts->onEachSide(5)->links() }} |
Filesystem Read / Write Steams
There is a new method for FileSystem integration in laravel 5.7
1 2 3 4 |
Storage::disk('s3')->writeStream( 'destination-doc.zip', Storage::disk('local')->readStream('source-doc.zip') ); |
These are the some major changes in laravel 5.7. Other than this there are some improvements in Error messages and directory changes inside the resources/views folder. Many files are moved to their own directory to solve the jumbled up problems.
Thanks for reading this article. Have you encountered any other changes, make sure to mention them in comments below.
Leave a Reply