Cart 0
No products in the cart.

Main Features of Laravel 8

Main Features of Laravel 8

 

Laravel 8, which was released in September 2020, is the latest stable version of Laravel. This version includes many new features including Laravel Jetstream, a models directory, model factory classes, migration squashing, rate-limiting improvements, time testing helpers, dynamic blade components, and many more features.

Here at Fixwebnode, as part of our Website Development Services, we regularly help our Customers to perform related Laravel Website development queries.

In this context, we shall look into the main features of Laravel 8.

 

What's New with the relaease of Laravel 8 ?

 

1. Change Path Of Default Models Directory

In the older versions of Laravel (5, 6.x, 7.x), the models were located in the app directory. But in Laravel 8, models are established in the "App/Models/" directory.

Model Path In Laravel 8:

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

 

2. Controllers Namespace Prefix Removed

The $namespace variable prefix from RouteServiceProvider.php file is removed. In the previous versions (like 6.x, 7.x), the $namespace variable prefix is available in the RouteServiceProvider.php file. Now, the "App\Http\Controllers" namespace is automatically added to the controller file.

The Old RouteServiceProvider.php file looks like this:

<?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';

In Laravel 8, the RouteServiceProvider.php file looks like this:

<?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. The php artisan serve function improved

In previous Laravel version like 6.x, 7.x if you modify something in .env file, then you have to restart the serve command to effect changes. But in Laravel 8, there is no need to restart the serve command as the changes are automatically effected without any further action.

 

4. Rate Limiting improved

In Laravel 8, you can define rate limit for your application route. If someone tries to fire too many request on server. this might take it down. So, you can set a limit to control the fire request on your laravel application.

Also, in laravel 8, you can apply rate limit to middleware. With this capability, you can set up a number of request per time.

To Rate Limit on RouteServiceProvider.php, below is a sample:

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

To enable the Rate Limit in Routes, modify as per below:

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. Route Caching Improved

In Laravel version 8, the route-cache issue has been resolved.

 

6. Pagination Design Updated

In Laravel 8, the default pagination has been removed. So if you want to use the default pagination class, then you have to add "useBootstrap()" in AppServiceProvider file.

You can use the below 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. Syntax for Event Listeners Changed

The syntax to call event listeners has been changed.

The Old syntax was:

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

The new one is:

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

 

8. Laravel Factory Introduced

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.

Have a look at the below code:

//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 version now has a Queued job batching feature. With this, you can pass all jobs to the queue at once as batch using Bus::batch() and wait until all jobs is finished.

Check the below code:

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

Laravel 8 has a backoff() function with which you can use to set up an array when defining a Queue job class.

Take a look at the below code:

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

In some cases, a user can create or update any post of a Laravel blog. If you need to notify administrative by sending a push or email notification which can take some time to send. Here, you can use model event with queueable as shown below:

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 Laravel 8 version, you can use the following command to use secret and ignore cookie which will allow your website to be with the old version visible to website users:

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

To test this, you can run the below url on your web browser:

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

 

13. Maintenance mode: pre-rendered page

Sometime, your website may down or having server issue. In this case, you can display a back soon page.

If you want to render back soon page until the website is up and running, you can simply use the following command:

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

Alternatively, you can use:

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

 

14. Dynamic Blade Components

In Laravel 8, you can add dynamic blade components as follows:

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

 

[Need to fix Laravel Website issues ? We can help you. ]

 



CONCLUSION

This article covers all about the new features in the Laravel 8 Version.


icon

Sign up to Newsletter

...and receive a free support ticket.