What is new in Laravel 8?

  8398 views   1 year ago Laravel

Hello artisan,

I have some good news for you. now finally laravel ream announces the release date of laravel 8. laravel 8 will be released on 8th September 2020. here I am sharing with you what is new in laravel 8 version.

here is a listing of what new functionality comes with laravel 8.

1.) Laravel Jetstream
2.) Schema Dump
3.) Time Traveller
4.) Dynamic Blade Component
5.) Default app/Models Directory for Model
6.) Enhancements on php artisan serve
7.) Removed Controllers Namespace Prefix
8.) Enhanced Rate Limiting
9.) Enhanced on Route Caching
10.) Update on Pagination Design
11.) Update Syntax for Closure Based Event Listeners
12.) Queueable Model Event Listeners
13.) Maintenance mode: secret access
14.) Maintenance mode: pre-rendered page
15.) Queued job batching
16.) Queue backoff()
17.) Laravel Factory

Now we see all the changes in details with the example

1.) Laravel Jetstream

Laravel Jetstream is a new Laravel ecosystem and it will give you more beautiful views for laravel application. it this free and open source so, anyone can use freely in laravel application. in this ecosystem, laravel provide by default some functionality like profile management, two-factor authentication, API token, team management, multi-session management. this ecosystem uses Tailwind CSS and Livewire or Inertia.

2.) Schema Dump

In laravel 8 we got the new artisan command php artisan schema:dump. it will help the developers like youu have large application and then you have lots of migration file in migration directory. but after some time it will be very hard to manage all migration file. then this artisan command will be provide you make one single file of your migration files. you need a file of your schema in database/schema/{connection}-schema.mysql

3.) Time Traveller

Another small helper feature coming to Laravel 8 is the ability to fluently time travel in your tests. This will allow you to easily test for things like what happens when a free trial ends, or a next billing date, etc.

$this->travel(5)->minutes;
$this->get($route)->assertSee('Created 5 mins ago');

// Travel forward one year
$this->travel(1)->year;
$this->get($route)->assertSee('Created 1 year ago');

// Travel to the given date time
$this->travelTo($user->trial_ends_at);
$this->get($route)->assertSee('Your free trial is expired');

4.) Dynamic Blade Component

In laravel 8 version you can render your blade view dynamically. so, you can choose it application run-time and make it possible which blade component you want to render.

<x-dynamic-component :component="$yourComponentName" />

5.) Default app/Models Directory for Model

In laravel 8 we will see a change in the model path till laravevl 7 we all know the model's path is app folder but in laravel 8 all models will be placed in one new directory. here is an example.

app/Models/User.php
app/Models/Post.php

If you also want to keep the model on the app folder then you can remove "Models" folder and run artisan command to create model it will create a model in app folder.

6.) Enhancements on php artisan serve

In laravel 8 Enhancements on php artisan serve this is helpful new changes. because as you know when you run your application php artisan serve then if you change some in your .env file then you must be stope the php server and run this again php artisan serve then the .env changes apply on laravel application.

But in laravel 8 you don't need do this more.

7.) Removed Controllers Namespace Prefix

In laravel 8 remove $namespace variable prefix from app/Providers/RouteServiceProvider.php file from the old version. so, now it will take automatically takes "App\Http\Controller" namespace to the controller.

Old app/Providers/RouteServiceProvider.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 app/Providers/RouteServiceProvider.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'));
        });
    }  
	....

8.) Enhanced Rate Limiting

In laravel 8 this is a very helpful feature and I mostly like this one functionality in laravel 8. if you work with API  project and you want to prevent any route or API route for per minute request. like one user only calls 4 times any API in one minute. you also apply this functionality on file downloading. so, you can make rate limit on any route.

just make the following changes in RouteServiceProvide.php file

app/Providers/RouteServiceProvider.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','[email protected]')->->middleware('throttle:uploadFile');
Route::get('download','[email protected]')->->middleware('throttle:downloadFile');
  
/* or use it no group */
Route::middleware(['throttle:uploadFile'])->group(function () {
       
}); 

9.) Enhanced on Route Caching

Before your laravel old version if you cach route using "php artisan route:cach" then your all route caches in the application. but in the old version there one issue. after route cach and you add a new route then sometimes that route not work in application and we need to clear all route from the cach.

But in Laravel 8 this issue will be resolve.

10.) Update on Pagination Design

Now, laravel 8 will use the default front-end library as a tailwind framework. so the 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:

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();
    }
}

11.) Update Syntax for Closure Based Event Listeners

In laravevl 8 you will see the new syntax change in Event Listeners. see the changes.

Old :

Event::listen(OrderShipped::class, function(OrderShipped $event) { 

    // Do something

});

New :

Event::listen(function(OrderShipped $event) { 
    /* Do something */
});

12.) Queueable Model Event Listeners

In laravel 8 another great new feature that i love that. when you call any event of model like creating, update, updating, etc, evetn then also you can do it easily make queueable.

sometimes we want to do some tasks like sending mail. then now you easily do in the background with this new feature.

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 */
        }))
    }
      
}

13.) Maintenance mode: secret access

In the old laravel version if you want your application down and up by the following artisan command.

php artisan down
php artisan up

But when you want to show the website sill when you upload on the server to some person then you can not give.

Laravel 8 provide a way you can create a secret for grant access to lots of people. you can use a secret and ignore cookie, so until your website up they can access the old version.

php artisan down --secret=new-pass

now it will create a new route and if you access this route then it will ignore the cookie and access the website by the following URL:

https://www.example.com/new-pass

14.) Maintenance mode: pre-rendered page

Laravel 8 added a new option to show back soon page when your website is down. you can use the render option with the path of the file and it will show that file until up website:

php artisan down --render="errors::backSoon"

you can also use the command as like bellow:

php artisan down --redirect=/ --status=200 --secret=myPassword --render="errors::503"

15.) Queued job batching

In laravel 8 you will see one new feature in Queue Job Batching so you can add multiple jobs to the queue at once as a batch. there is a then(), catch() and finally() callback will be fire on all jobs will finished.

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();

16.) Queue backoff()

In the laravel 8 one another new feature regarding the queue. backoff() help of this function you can set the retry queue job values as an array.

class ExampleJob
{
    /**
    * Calculate the number of seconds to wait before retrying the job.
    *
    * @return array
    */
    public function backoff()
    {
        return [1, 5, 10];
    }  
    ....
}

17.) 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();
});

if you want to check all the new feature then please visit this link: https://laravel.com/docs/8.x/upgrade

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]