What is new in Laravel 8 ?

João Nascimento
5 min readSep 23, 2020

--

Laravel 8 is now released!!

In this tutorial, i will show you what’s new in laravel 8 version, like so…

  • Laravel Jetstream
  • Models Directory
  • Model Factory class
  • Route caching improvement
  • Maintenance Mode Improvements
  • Migration Squashing
  • Improved Rate Limiting
  • Schema Dump
  • Time Testing Helpers
  • Dynamic Blade Components
  • Job Batching
  • And many more features…

8th September Laravel 8 was launch, here is the documentation site.

Let’s see one by one in details as i can help you more.

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.

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.

Model Factory Classes

Eloquent model factories are now class-based starting in Laravel 8, with improved support for relationships between factories (i.e., a user has many posts). I think you’ll agree how awesome the new syntax is for generating records via the new and improved model factories:

use App\Models\User;User::factory()->count(50)->create();// using a model state "suspended" defined within the factory class
User::factory()->count(5)->suspended()->create();

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 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

Migration Squashing

If your application contains many migration files, you can now squash them into a single SQL file. This file will be executed first when running migrations, followed by any remaining migration files that are not part of the squashed schema file. Squashing existing migrations can decrease migration file bloat and possibly improve performance while running tests.

Improved Rate Limiting

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.

Time Testing Helpers

Laravel users have enjoyed full control over time modification via the excellent Carbon PHP library. Laravel 8 brings this one step further by providing convenient test helpers for manipulating the time within tests:

// 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 exact time...
$this->travelTo(now()->subHours(6));
// Return back to the present time...
$this->travelBack();

When using these methods, the time will reset between each test.

Dynamic Blade Components

Sometimes you need to render a blade component dynamically at runtime. Laravel 8 provides the <x-dynamic-component/> to render the component:

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

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;

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response