What's New in Laravel 8 | Laravel 8 New Features
Hi All,
In this tutorial, i will show you what's new in laravel 8 version. i will give you point wise laravel 8 new features and explain it how you can use it and what's remove in laravel 8.
8th September Laravel 8 will launch, Taylor Otwell announce in laravel website. so we are waiting for laravel 8 with new amazing feature. i read laravel website and upgrade guide and i found out some interesting new feature that love it.
So, here i will show you one by one some great features are coming in laravel 8 application. they added more enhancements on models, .env, rate limiting, route caching, factory, controller namespace etc.
Let's see one by one in details as i can help you more.
*) Default app/Models Directory for Model
Taylor Otwell tweet and announce to next laravel 8 version will have new directory "Models" for all Eloquent Model and i love this feature.
Currently all models are be default store in app folder like app/User.php, app/Post.php etc. but laravel 8 provide new directory "Models" to store all model on that folder so it will looks like as bellow:
If you run this command "php artisan make:model Post" then it will store as like bellow:
Old Model Path:
app/User.php
app/Post.php
New Model Path:
app/Models/User.php
app/Models/Post.php
If you also want to keep model on app folder then you can remove "Models" folder and run artisan command to create model it will create model in app folder.
*) Enhancements on php artisan serve
This feature also announce via tweet and i can show you what is benefit of this feature. when you working locally. you already run provide via bellow command:
php artisan serve
and you need to change some variable value in .env file, then you have to restart again your serve command now. but laravel 8 provide way that you can not need to restart again serve command. variable value will reflect automatically.
So, also love this feature because we don't need to restart again and again when we change .env file values.
*) Removed Controllers Namespace Prefix
Laravel 8 removed $namespace variable prefix from RouteServiceProvider.php file from previous version of laravel. so basically it will automatically apply "App\Http\Controllers" namespace to controller.
you can see old RouteServiceProvider.php and new RouteServiceProvider.php file.
Old RouteServiceProvider.php:
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* The path to the "home" route for your application.
*
* @var string
*/
public const HOME = '/home';
....
New RouteServiceProvider.php:
<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/home';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->group(base_path('routes/web.php'));
Route::prefix('api')
->middleware('api')
->group(base_path('routes/api.php'));
});
}
....
*) Enhanced Rate Limiting
Another great feature by laravel 8. you can define rate limit to your route. if you are building api project or some ERP project then you have to set limit to fire request on your api or download files otherwise it may down our server if someone fire so many request.
So, laravel 8 provide rate limit middleware there you can set up number of request per time. i will show you how you can setup with laravel 8 bellow:
RouteServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/home';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
RateLimiter::for('uploadFile', function (Request $request) {
Limit::perMinute(8)->by($request->ip()),
});
RateLimiter::for('downloadFile', function (Request $request) {
if ($request->user()->isSubscribed()) {
return Limit::none();
}
Limit::perMinute(8)->by($request->ip()),
});
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->group(base_path('routes/web.php'));
Route::prefix('api')
->middleware('api')
->group(base_path('routes/api.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60);
});
}
}
Use Rate Limit
Route::get('upload','FileController@index')->->middleware('throttle:uploadFile');
Route::get('download','FileController@index')->->middleware('throttle:downloadFile');
/* or use it no group */
Route::middleware(['throttle:uploadFile'])->group(function () {
});
*) Enhanced on Route Caching
We almost use laravel cache for view, config and route. as for route we are using "php artisan route:cache" command to cache route.
In previous version of laravel, you can cache route and then after if you add new routes on route file then it closure issue. But laravel 8 solve that issue, if you will add new route then it will add it automatic on cache file.
*) Update on Pagination Design
Now, laravel 8 will use default front-end library as tailwind framework. so default pagination class is removed. but if you want to use it then you have to call "useBootstrap()" in AppServiceProvider file.
you can use as bellow:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Paginator::useBootstrap();
}
}
*) Update Syntax for Closure Based Event Listeners
Laravel 8 changed syntax to call event listeners. so i can show you bellow example with old and new syntax:
Old:
Event::listen(OrderShipped::class, function(OrderShipped $event) {
// Do something
});
New:
Event::listen(function(OrderShipped $event) {
/* Do something */
});
*) Queueable Model Event Listeners
Another new great feature that i love it. when you call model creating, created, updating, updated etc event then also you can do it easily queueable.
Sometime, we are sending email notification to admin when create new product so it may take time to send email. so in backend you can use laravel eloquent model event with queueable.
Let's see example:
Product Model:
class Product extends Model {
protected static function booting()
{
static::created(queueable(function(Product $product) {
/* Write something Here */
}))
static::updated(queueable(function(Product $product) {
/* Write something Here */
}))
}
}
*) Maintenance mode: secret access
If you want to website down and up then we was using following command:
php artisan down
php artisan up
But when you want to show website sill when you upload on server to some person then you can not give.
Laravel 8 provide way you can create secret for grant access to lots of people. you can use secret and ignore cookie, so until your website up they can access old version.
php artisan down --secret=new-pass
now it will create new route and if you access this route then it will ignore cookie and access website by following url:
https://www.example.com/new-pass
*) Maintenance mode: pre-rendered page
Laravel 8 added new option to show back soon page when your website is down. you can use render option with path of file and it will show that file until up website:
php artisan down --render="errors::backSoon"
you can also use command as like bellow:
php artisan down --redirect=/ --status=200 --secret=myPassword --render="errors::503"
*) Queued job batching
Laravel 8 provide new feature Queued job batching so you can add multiple jobs to the queue at once as batch. there is a then(), catch() and finally() callback will be fire on all jobs will finished.
you can call multiple jobs as like bellow:
Bus::batch([
new SendMailJob(),
new SendMailJob(),
new SendMailJob(),
new SendMailJob(),
new SendMailJob(),
])->then(function(Batch $batch){
/* All jobs completed successfully */
})->catch(function(Batch $batch) {
/* First job failure detected */
})->finally(function(Batch $batch) {
/* All jobs have finished executing */
})->dispatch();
*) Queue backoff()
Laravel 8 added new method backoff() when you can define in your Queue job class. you can define number time with array to set for retry after.
you can call job as like bellow:
class ExampleJob
{
/**
* Calculate the number of seconds to wait before retrying the job.
*
* @return array
*/
public function backoff()
{
return [1, 5, 10];
}
....
}
*) Laravel Factory
Laravel 8 model improve on factory, so you can easily create new dummy records from factory. they added new times() that way you can define number of records created option. so you can see bellow improvement:
Route::get('test-factory',function(){
return User::factory()->create();
});
Route::get('test-factory',function(){
return User::factory()->times(10)->create();
});
There are more feature laravel 8 added, but i saw and what i like i just explain you. you can also see laravel 8 upgrade guide from here: Laravel Upgrade Guide.
I hope it can help you.
Thank you.
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Multi Step Form Example Tutorial
- PHP Laravel Left Join Query Example
- Laravel 6 Release New Features and Upgrade
- Laravel 5.8 New Features List
- Laravel Validation for Multiple Files in Array Example
- Laravel 5.7 - New Notification System Tutorial for Beginner
- Laravel 5.5 New Feature - BladeIf Directive Example
- Laravel 5.5 - Validation Data Return - New Feature
- Laravel 5.4 New Feature - Add New Components & Slots in Blade Template
- Laravel 5.4 New Feature - Add Eloquent WhereKey Method Example
- Laravel $loop Variable New Feature Example