New in Laravel 8

arifpavel wrote on October 27, 2020
Laravel 8

Laravel 8 officially released on 8th September 2020. The laravel team releases new Laravel version in every 6-month interval with major changes. As Laravel 8 Non-LTS (general version), the Laravel 8 will provide 6 months bug fixes until March 8, 2021, and 1-year security fixes until 8 September 2021. In Laravel 8, we will get many new features and improvements. Let’s explore what’s new in Laravel 8.

At a glance Laravel 8

VersionLaravel 8 (Non-LTS)
Bug fixesUntil March 8, 2021
Security fixesUntil Sep 8, 2021
Release Date8th Sep 2020

New in Laravel 8

Laravel 8 has some new features like Job Batching, New model directory, Schema Dump, Laravel Jetstream and enhancement of the previous features like route caching, maintenance mode, rate limiting and more bug fixes. Let’s have a look at what are new things and improvement in Laravel 8.

Job Batching

Job batching is now easier with new Bus::batch() . It is one of the most exciting features of Laravel 8. Just pass your all jobs into a Bus::batch() and wait for the response. Here is an example.

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

The response will tell you the statistics of your dispatched job. Here is an example of a response.

batch-job-response.png

To find the batch job details you can use Bus::findBatch($batchId)

New Model Directory

From Laravel 8, the default model directory in app/Models. Before Laravel 8, all the models were in-app directory which was really messy when lots of the model consists in our application. Now Laravel 8 default model directory make it more organized.

Laravel Jetstream

Laravel Jetstream is a new Laravel ecosystem. It gives you beautiful scaffolding for laravel application. It is completely free and open-source. It has in-build features like user profile management, Two-factor Authentication, API tokens, Team management, Multi-Session Management and a lot of cool stuff. Jetstream design with Tailwind CSS and you can choose scaffolding with Livewire or Inertia.

Laravel Factory

Laravel 8 provides an easier way to seed model data with the new factory improvement. Let’s have a look at how cool the factory is now.

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

Schema Dump

New artisan command added for schema dump php artisan schema:dump. It will help the developers from having a huge migrations directory full of files they no longer need and it’s quicker (loading a single schema file) then running hundreds of migrations file. You need a file of your schema in database/schema/{connection}-schema.mysql

Rate Limiting – Improved

In laravel 8, now you can rate limit in a new way, easily and more conveniently. Here is an example

Define Rate Limit

RateLimiter::for('upload', function (Request $request) {
	Limit::perMinute(10)->by($request->ip()),
});

RateLimiter::for('download', function (Request $request) {
	if ($request->user()->isSubscribed()) {
    	return Limit::none();
	}
	Limit::perMinute(10)->by($request->ip()),
});

Use rate limiter as regular middleware

Route::get('/upload','UploadController@index')->->middleware('throttle:upload');
Route::get('/download','DownloadController@index')->->middleware('throttle:download');

// or use it no group
Route::middleware(['throttle:upload'])->group(function () {
	
});

Maintenance Mode – Improved

Until Laravel 8, we used php artisan down for temporary down our website for maintenance but the previous implementation was the entire framework has to be booted in order to render the maintenance page. In Laravel 8, it has been done more efficiently.

php artisan down --secret=myByPassSecret

Once you down the website with secret bypass. Now you can use it to check your website something like this. The maintenance middleware will intercept this request and issue a maintenance mode bypass cookie to the user and redirect to /.

http://example.com/myByPassSecret

You can set more into maintenance mode options.

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

Explanation
Put the application offline
Redirect all routes to “/”
Set status code
Set a secret to bypass maintenance mode
Render a view file for the downtime

Time Traveller

With laravel time traveller helper now you can more easily manipulate time. Laravel base features test class included these helpers. Here is some example.

//goto to future
$this->travel(10)->seconds();
$this->travel(10)->minutes();

//back to past
$this->travel(-8)->seconds();
$this->travel(-8)->minutes();

Dynamic Blade Component

From laravel 8 you can render dynamic blade component. So that you can choose at application run-time which component will be rendered.

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

Default pagination

Laravel 8 pagination will use Tailwind CSS by default.

Route Caching – Improved

From Laravel 8, you can cache your routes even you use closure in route definition.

Route::get('/about',function(){
  return view('about');
});

php artisan serve – improved

Until Laravel 8 we had to restart our laravel application by the php artisan serve command in every time when we made a change in the .env file. In laravel 8 you don’t have to re-run the php artisan serve command even we change our .env file.

Credit: Laravel News