Laravel 8 New Features | Release Notes

Laravel 8 New Features | Release Notes

Today (September 8, 2020), laravel 8 latest version has been launched with great new features. Below, You can see that laravel 8 new features.

Note that, In every 6 months laravel release a new version (6.x, 7.x, 8.x).

What’s New Laravel 8 New Features

New features of Laravel 8 are given below. Along with that, there is an explanation about them in detail:

  • 1 – Change Path Of Default Models Directory
  • 2 – Removed Controllers Namespace Prefix
  • 3 – Enhancements on php artisan serve
  • 4 – Enhanced Rate Limiting
  • 5 – Improved on Route Caching
  • 6 – Update on Pagination Design
  • 7 – Modify Syntax for Event Listeners
  • 8 – Laravel Factory
  • 9 – Queued job batching
  • 10 – Queue backoff()
  • 11 – Queueable Event Listeners In Models
  • 12 – Maintenance mode: secret access
  • 13 – Maintenance mode: pre-rendered page
  • 14 – Dynamic Blade Components

1 – Change Path Of Default Models Directory

In the previous laravel version (5, 6.x, 7.x) models are located inside the app directory. But in laravel 8 models are establishing inside App/Models/ directory:

New Model Path In Laravel 8:

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

2 – Removed Controllers Namespace Prefix

Removed $namespace variable prefix from RouteServiceProvider.php file in Laravel. In previous like 6.x, 7.x , $namespace variable prefix available on RouteServiceProvider.php, that’s why it was automatically added “App\Http\Controllers” namespace on controller file.

Old RouteServiceProvider.php file looks like following:

<?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 file looks like following:

<?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'));
        });
    }
  
....

3 – Enhancements on php artisan serve

In previous laravel version like 6.x, 7.x if you change something in .env file, then you have to restart again your serve command. But in laravel 8 you can not need to restart again serve command.

4 – Enhanced Rate Limiting

In laravel 8, you can define rate limit for your application route. If someone fire so many request on server it may down. So, you can set limit to fire request on your laravel application.

Also, you know that, laravel 8 provide rate limit middleware. Using this middleware, you can set up number of request per time.

Add Rate Limit on 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 with Routes:

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

5 – Improved on Route Caching

In laravel version 8 improved new route-cache issue.

6 – Update on Pagination Design

IN laravel 8 version, default pagination has been removed. But if you want to use the default pagination class, then you have to add “useBootstrap()” in AppServiceProvider file.

See the following AppServiceProvider.php file:

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

7 – Modify Syntax for Event Listeners

In Laravel 8, modify the syntax to call event listeners.

See the following new and old syntax:

New Syntax:

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

Old Syntax:

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

    // Do something

});

8 – Laravel Factory

In Laravel 8, You can add new dummy records from factory in database. Because it has been added new times() that way you can define number of records created option.

See the following:

//whiteout time
Route::get('user-factory',function(){

   return User::factory()->create();

});

//with time
Route::get('user-factory',function(){

   return User::factory()->times(10)->create();

});

9 – Queued job Batching

This is very great. Becuase it has been add new feature Queued job batching, So you can pass all jobs to the queue at once as batch using Bus::batch(). And wait to finished all jobs.

Bus::batch([
    new Job1(),
    new Job2()
])->then(function (Batch $batch) {
    if ($batch->hasFailures()) {
        // die
    }
})->success(function (Batch $batch){
    //invoked when all job completed

})->catch(function (Batch $batch,$e){
    //invoked when first job failure

})->allowFailures()->dispatch();

10 – Queue backoff()

In Laravel 8, One new function is added named backoff(). If you want define Queue job class. You can set with array.

See the following:

 
class ExampleJob

{

    /**

    * Calculate the number of seconds to wait before retrying the job.

    *

    * @return array

    */

    public function backoff()

    {

        return [1, 5, 10];

    }

    

    ....

}

11 – Queueable Event Listeners In Models

Sometime, any user create or update any post on blog. And you want to notify administrative.

At that time, you will send push or email notification, so it take some time to send push and email notification. So, you can use model event with queueable.

See the following example

class Post extends Model {
  
    protected static function booting() 
    {
        static::created(queueable(function(Post $post) {
           /* Write something Here  */
        }))
  
        static::updated(queueable(function(Post $post) {
           /* Write something Here */
        }))
    }
      
}

12 – Maintenance mode: secret access

In new Laravel 8 version, you can use the following command to use secret and ignore cookie, so until your website up they can access old version.

php artisan down --secret=new-access-pass

You can fire this url on your browser and check it out:

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

13 – Maintenance mode: pre-rendered page

Sometime, your website may down or any server issue. And at that time, you can displlay back soon page.

If you want to render back soon page until up website, use the following command:

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

Also like:

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

14 – Dynamic Blade Components

In laravel 8, you can also add dynamic blade components like the following:

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

I hope you enjoyed this post of what’s new features in the Laravel 8 Version.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *