Latest

6/recent/ticker-posts

What's new in Laravel 8? What are the new features in Laravel 8?

 

As Laravel follows semantic versioning Scheme, every new major framework releases are released every six months.


Version Release Bug Fixes Until Security Fixes Until
8 September 8th, 2020 March 8th, 2021 September 8th, 2021

New Features in Laravel 8

Now lets direct dive into the cool features added in laravel 8. The following are the main features added in laravel 8:

    1. Jetstream
    2. Model Directory
    3. Model factory classes
    4. Migration squashing
    5. Job batching
    6. Improved rate limiting
    7. Queue improvements
    8. Dynamic Blade components
    9. Tailwind pagination views
    10. Time testing helpers
    11. Improvements to artisan serve
    12. Event listener improvements 
    13. Bug fixes and various improvements

★Jetstream

One of the super cool feature added in laravel 8 is Jetstream. It is a very beautifully designed scaffolding for laravel authentication. It provides a modern authentication system such as two-factor authentication, session management, email verification, avatar upload, and API support. Laravel Jetstream is the great replacement over legacy authentication UI scaffolding of the previous laravel version.

★Model Directory

This is another absolutely cool feature in laravel 8. By default, all the eloquent models were inside the app directory in all previous laravel version before laravel 8. So laravel creates the poll to the user on twitter by asking them whether the model should be located. According to most users' demand in laravel 8, all the eloquent models will be located inside the app/models directory which is very cool.

★Model Factory Classes

In laravel 8 all the eloquent model factories have been entirely re-written as class-based factories and improved to have first-class relationship support.

UserFactory included with Laravel is written like so:

class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
            'email_verified_at' => now(),
            'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }
}

★Migration Squashing

Suppose you are maintaining the projects for many years, the migration directory will grow and grow larger. Let us consider you have 100 migration files, and each of those has to run every time migrate:fresh which is very time consuming and waste.So migration squashing comes here. Laravel 8 can help us with this through a new command called schema:dump this effectively allows us to squash all the migration files down into a single schema file. This is really helpful. Three new commands are:

php artisan schema:dump 

php artisan schema:dump --prune // dump the current database schema  and prune all existing migration files 

Now each time when we run migrate and migrate:fresh, first go through the schema file on top and then to others. After this command, this will create a schema file into new directory database/schema.

★Job Batching

Another cool feature added in laravel 8 is Job Batching. In laravel 8, it allows us to execute a batch of jobs and perform some action when the batch of the job has completes executing. Bus facade is used to dispatch a batch of jobs. 
Let's see the example:


Bus::batch([
    new JobA(),
    new JobB()
])->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();

★Improved rate limiting

This will be very beneficial in laravel 8. When attackers or any robot try to request for large no of time down the server, it will prevent this from being requested except limit.

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;

RateLimiter::for('global', function (Request $request) {
    return Limit::perMinute(1000);
});

★ Dynamic Blade Components

This is absolutely the cool feature we are awaiting for. It allows us to render the component dynamically  in run time. Suppose you have a webpage which two components one for adults and one for children. Then you can dynamically render this with the help of this:

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

★ Tailwind pagination views

You may know the Tailwind CSS is a very beautiful, low level, customizable CSS framework. In laravel 8 default pagination views will use tailwind CSS.

★ Time testing helpers

When testing the larvel application we may be required to modify the time then it will help us to do so.

public function testTimeCanBeManipulated()
{
    // Travel into the future...
    $this->travel(5)->milliseconds();
    $this->travel(5)->seconds();
    $this->travel(5)->minutes();
    $this->travel(5)->hours();
    $this->travel(5)->days();
    $this->travel(5)->weeks();
    $this->travel(5)->years();

    // Travel into the past...
    $this->travel(-5)->hours();

    // Travel to an explicit time...
    $this->travelTo(now()->subHours(6));

    // Return back to the present time...
    $this->travelBack();
}

★ Improvements to artisan serve

This is one of my favorite feature added in laravel 8. In the previous version when we do some changes in the .env file then we had to stop the command and re-run manually. But in laravel 8 artisan serve will automatically do this. It will automatically reload the application when new changes detected in the .env file.

★ Event Listeners Improvements

Closure based event listeners may now be registered by only passing the Closure to the Event::listen method. Laravel will inspect the Closure to determine which type of event the listener handles:

use App\Events\PodcastProcessed;
use Illuminate\Support\Facades\Event;

Event::listen(function (PodcastProcessed $event) {
    //
});

In addition, Closure based event listeners may now be marked as queueable using the Illuminate\Events\queueable function:

use App\Events\PodcastProcessed;
use function Illuminate\Events\queueable;
use Illuminate\Support\Facades\Event;

Event::listen(queueable(function (PodcastProcessed $event) {
    //
}));

All the features here I mentioned and discussed depend on Laravel official documentation and Laracon Online 2020 conference. You can see in detail about the laravel 8 features in Laravel Official Documentation.



Post a Comment

0 Comments