As we all know 8th September was the official release date for Laravel 8. And we all are excited to witness the upcoming new features and improvements. Laravel 8 has come up with the improvements made in Laravel 7.x with an introduction to Laravel Jetstream, a Models Directory, Model Factory Classes, Migration Squashing, Rate-limiting improvements, Time Testing Helpers, Dynamic Blade Components, and many other features with a variety of bug fixes and usability improvements.
So, before moving to Laravel 8’s new features, it’s necessary to point out that starting with the release of the Laravel 6 version, Laravel now follows semver and they will release the new major releases every 6 months. Just for your reference, how the Laravel Release process works.
Laravel Jetstream improves upon the legacy authentication UI Scaffolding available in previous versions of Laravel. It provides a perfect starting point for your next projects including login, registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management.
Laravel 8 will provide a default app/models directory, rather than leaving the model class in the root app directory as in previous versions of Laravel.
Right now, According to a poll, over 80% of developers were creating an app/models directory themselves anyway. It will be very helpful to the developers.
You can create a model using artisan command like
php artisan make:model modelName
If you want to keep your models in your app directory then delete the models directory, the generator commands will respect that and create the model classes in your app directory.
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();
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.
Laravel 8 brings improvements to existing rate limiting functionality while supporting backward compatibility with the existing throttle middleware and offering far more flexibility. Laravel 8 has the concept of Rate Limiters that you can define via a facade:
use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Support\Facades\RateLimiter; RateLimiter::for('global', function (Request $request) { return Limit::perMinute(1000); });
As you can see, the for()
method takes the HTTP request instance, giving you full control over limiting requests dynamically.
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.
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" />
Usually, when route caching is enabled, a serialized output of the routes file is generated, i.e. all is compiled into a big PHP array. Currently, routes that are defined as in the example below couldn’t be cached.
Route::get('foo', function () { return 'Hello World'; });
Laravel 8 will provide add closures routes into the cache.
Nested component attributes are now allowed to be merged.
Example:
<x-button {{ $attributes->merge(['class' => 'bg-red']) }}> {{ $slot }} </x-button>
In Laravel 8, you’ll be able to skip the first definition of the event class as the framework will be able to infer it from the type hinted argument – e.g.
In Previous Laravel Version:
Event::listen(UserRegistered::class, function(UserRegistered $event) { // Do something });
In Laravel 8:
Event::listen(function(UserRegistered $event) { // Do something });
In Laravel 7, if you run php artisan down to put your site into maintenance mode while deploying your application and then run Composer as part of the deployment, your application may still throw errors while your dependencies are changed and the autoload file is written – which means end users will see an error page, rather than the maintenance mode page.
That’s solved in Laravel 8! You can now pass the name of a view to a “render” as part of the artisan down command
E.g.:
php artisan down --render="errors::back-soon"
If anyone then attempts to access the site, they will see the pre-rendered view, meaning errors won’t be thrown.
php artisan down —secret=laracon-2020
Now we can access the route as a secret. We can use application routes even during maintenance mode using the command mentioned above. There will be several options available in the artisan down command like: render, redirect, status, and the secret to ensure much more control.
Now we can queue a bunch of jobs that trigger at the same time, and then it will trigger callbacks after the entire batch is finished.
There are 3 callbacks available:
then() – this will fire when all jobs in the batch have completed successfully
catch() – this will fire on the first (if any) job failure in the batch
finally() – this will fire when all jobs in the batch have finished executing (some may have completed successfully, some may have failed)
E.g.:
Bus::batch([ new BatchedJob, new BatchedJob, new BatchedJob, new BatchedJob, new BatchedJob, ])->then(function (Batch $batch) { info('All Jobs completed Successfully'); })->catch( info('First Batch job failure detected'); )->finally( info('The batch has finished executing'); )->dispatch();
Failed items also can be caught without interruption.
Real-time progress can be seen of this batched jobs:
Bus::findBatch('')
Whilst it’s been possible to send a closure to the queue for background process for some time, handling the failure of these jobs wasn’t ideal.
Whilst it would go to the failed jobs table in your database, you couldn’t execute any other code once the failure occurred.
In Laravel 8, you’ll be able to register a callback to run when the job has failed, via a catch() method – similar to the failed() method on a standard job class.
E.g.
dispatch(function() { // Do something... })->catch(function(Throwable $e) { // React to / handle the error });
In Laravel 7 if you update your env file you have to restart your server.
Laravel 8 provides such a feature like, If you update your env file you don’t have to restart your server it will automatically restart the server.
So, here we have seen a few features and improvements. To see a complete list check out the Laravel 8 release notes. If you face any problem while upgrading your existing Laravel to Laravel 8 version, feel free to contact our Laravel Experts.
Subscribe to our newsletter and learn about the latest digital trends.