What is new in Laravel 8?

What is new in Laravel 8?

Written by Bobby Iliev on Sep 4th, 2020 Views Report Post

Introduction

As of writing this post, Laravel 8 is still not officially out, but it was just announced at Laracon Online.

Here is a list with some of the new excellent features that you will get with the new version 🔥

Models Directory

Not long ago Taylor Otwell created a poll on Twitter asking the community if they were putting all models in an app/Models folder or use the default app/ directory, and the majority said that they put their models in app/Models.

Thanks to that poll, the app/Models folder is now going to be included by default in Laravel 8.

If you create a new model with the php artisan make:model SomeModel command, it will put your new model in the app/Models directory.

A remarkable thing about this is that if you don't like this Models directory and if you were to delete the app/Models directory, the artisan command will add your new model in the app/ directory directly as it used to be in all previous versions of Laravel.

Route caching improvement

How route caching works is that it takes all of your routes and puts them in a big PHP Array, which is more efficient in terms of speed.

In the past, if you had a closure in your routes file, the route caching would not have worked, but now with Laravel 8, even if you have a closure in your routes, the route caching will work just fine!

That way, you can always cache your routes!

Maintenance Mode Improvements

There have been some pretty nifty improvements to the php artisan down command.

With the previous versions, in order to allow only some people to access the website, you had to use the white list feature as described in this post here:

How to Create Custom Laravel Maintenance Page

With Laravel 8, rather than using an IP white list, you can have a secret. To configure that, all you have to do is:

php artisan down --secret=YOUR_SECRET_HERE

Make sure to change the YOUR_SECRET_HERE part with a secure string!

Then to access the site that is in maintenance mode, visit yourdomain.com/YOUR_SECRET_HERE and this will generate a secret cookie which would allow you to browse the website just as usual!

That is a great way to put your website in a maintenance mode, but still, allow some people to access it if they had to!

Another great addition to the php artisan down command is that you could pre-render your maintenance page, so that even if you run composer update your end users would still see your maintenance page rather than some errors.

To do that just run:

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

That way, you can carry out extensive maintenance without having to worry about your users seeing some odd errors rather than a friendly-looking maintenance page.

Cool is that you can combine the flags together. For example, you can run the following command to add a secret along with a rendered page and change the status code at the same time:

php artisan down --render="errors::503" --status=200 --secret=YOUR_SECRET_HERE

Rate Limit Improvements

There is a new way to define a rate limit in Laravel 8. It has more flexibility, and at the same time, it is still compatible with the previous release's throttle middleware API.

How it works now is first you specify the rate limiter:

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

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

Then after that, you can use that RateLimitter as a standard middleware:

Route::middleware(['throttle:login'])->group(function () {
    Route::post('/login', function () {
        //
    });
});

This is very handy for APIs as well, where you could limit your users on how many requests they can send per minute, for example.

Schema Dump

The Schema Dump feature is probably my favorite one, as I can use it easily with Laravel Voyager!

The artisan command that you need to use is:

php artisan schema:dump

The above command will generate a schema file in the database/schema directory, which is basically the structure of your whole database.

You can also dump your current database schema and then prune all existing migrations by adding the --prune flag:

php artisan schema:dump --prune

The above will remove all of your old migrations and generate a single schema dump file. This is an excellent way of clearing your migrations files in case you have a lot of them!

After that, you can start adding new migrations again, and it will work as usual. And once you reach a certain number and want to clear the migrations folder, you can run the schema:dump command again and get it updated.

Laravel Jetstream

Laravel Jetstream is a new application scaffolding for Laravel. It is free and opensource.

Jetstream gives you a better starting point for your new projects. It includes the following components:

  • Login and registration functionality
  • Email verification
  • Two-factor authentication
  • Session management
  • API support via Laravel Sanctum

Laravel Jetstream replaces the legacy Laravel authentication UI available for previous Laravel versions.

Jetstream uses Tailwind CSS, and you can choose between Livewire or Inertia.

Job Batching

I believe that one of the most notable improvements is the Laravel's job batching feature.

It now allows you to run a batch of jobs and, after that, run some action once the batch of jobs has completed executing. Example:

use App\Jobs\ProcessPodcast;
use App\Podcast;
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Batch;
use Throwable;

$batch = Bus::batch([
    new ProcessPodcast(Podcast::find(1)),
    new ProcessPodcast(Podcast::find(2)),
])->then(function (Batch $batch) {
    // All jobs completed successfully...
})->catch(function (Batch $batch, Throwable $e) {
    // First batch job failure detected...
})->finally(function (Batch $batch) {
    // The batch has finished executing...
})->dispatch();

return $batch->id;

For more information about job batching, you can go through the official [queue documentation here] (https://laravel.com/docs/8.x/queues#job-batching).

Conclusion

There are some other improvements like the default pagination now uses TailwindCSS, the php artisan serve command has been improved, new Model Factory Classes, and more.

It is absolutely fantastic to see how much effort is being put into Laravel.

For more information, make sure to check out the official release notes here:

https://laravel.com/docs/8.x/releases

Also make sure to install Laravel 8 and play with the new features! You can do this on a DigitalOcean Droplet. If you don't have an account already you can use my referral link to get $100 free credits if you are new to DigitalOcean:

DigitalOcean $100 Free Credit

Hope that this helps!

Comments (0)