DEV Community

Cover image for Laravel 8 Goodies - Part 1
Zubair Mohsin
Zubair Mohsin

Posted on

Laravel 8 Goodies - Part 1

Laravel 8 is the upcoming new version which will probably be released on September 8th, 2020.

Taylor Otwell showed us a lot of features from Laravel 8 on Laracon Online.

I originally created a Twitter thread about it which you can refer to ๐Ÿ‘‡๐Ÿผ

Let's get started!

1- Models directory

Laravel 8 will include the Models directory in the app directory.

app/Models

If you have a Models directory and you run artisan make:model command it will place your Model class in that directory. But, if you don't have Models directory it will put your Model class inside app directory as before.

2- Controller Router Namespacing

We used to have a $namespace property in RouteServiceProvider

protected $namespace = 'App\Http\Controllers';
Enter fullscreen mode Exit fullscreen mode

In Laravel 8, this hard-coded value will be removed so that you have full control over namespaces of your controllers.

This will not affect the upgrade process.

3- Improvements to Route Caching

php artisan route:cache command is great for performance in production. There was one problem that it didn't allow us to cache closure based routes. It was only meant for Controller based routes. It would throw an error even if you had one closure based route. In Laravel 8, you will be able to cache closure based routes too.

4- Blade Components Attributes improvements

These improvements are especially related to Nested components.
For example you have a button component:

<button {{ $attributes }}>
   {{ $slot }}
</button>
Enter fullscreen mode Exit fullscreen mode

and we wanted to extend that, e.g. a dangerbutton component:

<x-button {{ $attributes->merge(['class' => 'bg-red']) }}>
   {{ $slot }}
</x-button>
Enter fullscreen mode Exit fullscreen mode

As you can see that dangerbutton component is introducing its own attribute class to parent button component. It helps you make extensible components and is now possible in Laravel 8.

5- Minor improvements in Event Listening

Previously if we had to listen for an Event we would do something like:

Event::listen(OrderCreated::class, function(OrderCreated $event){
    info($event->whatever);
});
Enter fullscreen mode Exit fullscreen mode

As you can see we are providing event (OrderCreated) class at two places. In Laravel 8, this syntax is still valid. But, you can also just provide the callable and type-hint the event you want to listen to:

Event::listen(function(OrderCreated $event){
    info($event->whatever);
});
Enter fullscreen mode Exit fullscreen mode

6- Queueable Anonymous Event Listeners

Let's take an example of the Model created event listener inside booting method.

protected static function booting()
{
    static::created(function(User $user){
         info($user->name)
    });
}
Enter fullscreen mode Exit fullscreen mode

Previously, there was no way to queue this listener.
Laravel 8 introduces the very first namespaced function ๐Ÿ˜ฒ called queueable which allows you to queue these event listeners.
Check it out:

use function Illuminate\Events\queueable; ๐Ÿ˜ณ

protected static function booting()
{
    static::created(queueable(function(User $user){
        info($user->name)
    }));
}
Enter fullscreen mode Exit fullscreen mode

In next part, we will cover artisan down command improvements coming to Laravel 8.

Top comments (3)

Collapse
 
sirawit profile image
Dragodzs

awesome โ™ฅ

Collapse
 
ahtshamajus profile image
Ahtsham Ajus

Graet

Collapse
 
abel4545 profile image
Abel4545

hi