What is the difference Between older version with laravel8?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i will discus about what’s new in laravel 8 version. Here  i will give you point wise laravel 8 new features and also explain it how we can use it and also what’s remove in laravel 8.

So, now i will give you one by one some great features are newly coming in laravel 8 application. In Laravel new version laravel 8 add some more enhancements on models, .env file, rate limiting, route caching, factory, controller namespace etc.

In interview call it is one of the common questions from the interviewer. what is the new features in laravel 8.

1.Default app/Models Directory for Model

Before laravel 8 all the models are store default in app folder as like as app/User.php, app/Post.php etc. But in laravel 8 provide us a new directory as “Models” to store all the model on that folder.

If we 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

So if we also want to keep all model on app folder then we can remove the “Models” folder and then if run artisan command to create model then it will create model in app folder.

Enhancements on php artisan serve

Now here i will talk about Enhancements on php artisan serve. When we are working on locally. we have to need run server via bellow command:

php artisan serve

and if we need to change some variables value in .env file, then we must have to restart again our serve command in before laravel 8 version. But now in laravel 8 provide a way that we do not need to restart again serve by using command. The variable value will be reflect automatically.

So, we don’t need to restart again and again when we change .env file values.

Removed Controllers Namespace Prefix

Another one In Laravel 8 removed the $namespace variable prefix from the file RouteServiceProvider.php from previous all version of laravel. So basically it will automatically apply on “App\Http\Controllers” namespace to the controller.

Here we can see the old RouteServiceProvider.php and new then 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

This is another great feature in laravel 8. we can define the rate limit to our route. if we are building the api project or some ERP project then we have to set the limit to fire request on our api or download files otherwise it may be down our server if someone fire so many request at a time.

So, in laravel 8 provide us the rate limit middleware there we can set up the number of request per time. Here i will also show you how we can setup this with laravel 8 in the bellow example:

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 are almost everyone use laravel cache for the view, config and route. As for route we can use this by using “php artisan route:cache” command to cache the route.

Here in previous version of laravel, we can cache the route and then after if we add the new routes on route file then it have closure issue. But in laravel 8 solve that issue, if we are add the new route then it will add this automatic on cache file.

Update on Pagination Design

Now we will know about new one, laravel 8 will use a default front-end library as like tailwind framework. so here default pagination class is removed. But if we want to use it then we need to call the “useBootstrap()” in our AppServiceProvider file.

we can use this as like 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

In Laravel 8 changed the syntax to call an event listeners. so here i will show you this in the bellow example also 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

Here is the another new great feature that i like. when we are call model for creating, created, updating, updated etc event then also we can do it easily on queueable.

Sometimes, we need to sending email notification to admin when create any new product so at this times it may take time to send the email. So in the backend we can also use laravel eloquent model event with the queueable.

Let’s see the 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 we want to make site down and up. Then we can do this by using the following command:

php artisan downphp artisan up

But when we want to show site sill when we are upload on the server to some person then we can not give.

Now in Laravel 8 provide us a way we can create secret for grant access to lots of the people. we can also use secret and ignore cookie, So until our site up they can access the old version.

php artisan down --secret=new-pass

Then it will create a new route and if we access this route then it will ignore the cookie and access this site by following url:

https://www.example.com/new-pass
Maintenance mode: pre-rendered page

In Laravel 8 add a new option to show back soon page when our site is down. we can use the render option also with the path of file and it will show that the file until up the site:

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

we can also use this command as like bellow:

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

In Laravel 8 provide new a feature Queued job batching. So we can add the multiple jobs to the queue at once as batch. There is a then(), catch() and finally() callback will be fire for the all jobs will finished.

we can call the 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()

In Laravel 8 add another new method as name backoff() when we are define in our Queue job class. we can also define the number time with array to set for retry after.

we can call job as like as 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

And also in Laravel 8 model improve on factory, so we can easily create a new dummy records from factory. they are added the new times() on that way we can define the number of records created option. so we can see bellow improvement:

Route::get('test-factory',function(){ 
  return User::factory()->create();
});
Route::get('test-factory',function(){  
 return User::factory()->times(10)->create();
});

Read Also: How we can create virtual host in ubuntu apache?

Thanks for read. I hope it help you. For more you can follow us on facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →