Laravel 8 Goodies - Part 1
Tags: laravel, php
Published: 2020-08-28 06:16:34

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 👇🏼 {% twitter 1298659051895693312 %}

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`
```php
protected $namespace = 'App\Http\Controllers';
```
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:
```html
<button {{ $attributes }}>
   {{ $slot }}
</button>
```
and we wanted to extend that, e.g. a `dangerbutton` component:
```html
<x-button {{ $attributes->merge(['class' => 'bg-red']) }}>
   {{ $slot }}
</x-button>
```
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:
```php
Event::listen(OrderCreated::class, function(OrderCreated $event){
    info($event->whatever);
});
```
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:
```php
Event::listen(function(OrderCreated $event){
    info($event->whatever);
});
```
## 6- Queueable Anonymous Event Listeners
Let's take an example of the Model *created* event listener inside **booting** method.
```php
protected static function booting()
{
    static::created(function(User $user){
         info($user->name)
    });
}
```
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:
```php
use function Illuminate\Events\queueable; 😳

protected static function booting()
{
    static::created(queueable(function(User $user){
        info($user->name)
    }));
}
```

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