written by
Vincent Chen

What's New in Laravel 8?

Code 6 min read
happy boy laravel 8
Photographer: Jen Theodore | Source: Unsplash

What’s new in Laravel 8? Plenty!

Laravel 8 has introduced many new powerful features such as Laravel Jetstream, Job Batching, New Model Directory, and Schema Dump. Many existing features such as Route Caching, Maintenance Mode, Rate Limiting and Bug Fixes were also enhanced. Since the Laravel 8 release, many minor updates have also been released throughout.

It will provide 6 months of bug fixes and 1-year security fixes from the date of release. While minor and patch releases may be as often as every week.

Let’s go through them together with some of these awesome Laravel features.

New Models Directory in Laravel 8

Prior to Laravel 8, the default Laravel application skeleton had kept its models under the ‘app’ directory. After an overwhelming community demand, it now contains an ‘app/Models’ directory which definitely is a cleaner way to maintain all these models.

Furthermore, all relevant generator commands have also been updated assuming your models exist within the ‘app/Models’ directory if it exists. If the directory does not exist, the framework will simply just keep your models within the ‘app’ directory.

Laravel 8 New Jetstream

Laravel Jetstream is a beautifully designed replacement and improvements upon the legacy authentication UI scaffolding available for the previous versions of Laravel written by Taylor Otwell. This design is using Tailwind CSS and offers your choice of Livewire or Inertia scaffolding.

Laravel Jetstream

It provides many useful functionalities such as login, registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management.

Model Factory Classes

The Eloquent model factories have gone through some major changes. It is now class-based factories as well as improvements to have first-class relationship support.

With the new ‘HasFactory’ trait available on generated models, the model factory can be called upon like this:

use App\Models\User;
User::factory()->count(50)->create();

Due to the latest model factories written to support classes and is not compatible with Laravel 7.x style factories. However, for people who wants to continue using Laravel 7, there’s a plugin to keep the structure as it is.

Migration Squashing

To prevent your migration directory from becoming overloaded with potentially hundreds of migration. If your application uses MySQL or PostgreSQL, the new Laravel update allows you to ‘squash’ all your migrations into one single SQL file to your ‘database/schema’ directory.

When trying to attempt to migrate your database, Laravel will execute the schema file’s SQL first, then execute any remaining migrations that were not part of the schema dump.

This essentially makes the migration section much cleaner.

Job Batching with Laravel 8

Laravel 8's job batching functionality enables you to efficiently execute a batch of jobs and when the jobs have complete executing it will then perform some action.

Laravel 8’s new ‘batch’ method of the ‘Bus’ facade allows you to use it to dispatch a batch of jobs. Furthermore, it is most useful when combined with completion callbacks such as ‘then’, ‘catch’, and ‘finally’ methods. Every callback will receive an Illuminate\Bus\Batch instance when they are invoked.

$batch = Bus::batch([
new ProcessPodcast(Podcast::find(1)),
new ProcessPodcast(Podcast::find(2)),
new ProcessPodcast(Podcast::find(3)),
new ProcessPodcast(Podcast::find(4)),
new ProcessPodcast(Podcast::find(5)),
])->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();

Improved Rate Limiting

Laravel’s request rate limiter functionality has been improved with more flexibility and power. The functionality has also maintained its backwards compatibility with previous release’s throttle middleware API.

Laravel 8 Maintenance Mode Enhancement

Previously, to access the application which is in the ‘php artisan down’ maintenance mode may be bypassed using an ‘allow list’ of IP addresses. However, in Laravel 8 this has been made simpler with ‘secret’ and token. Now all people need is the secret key to view the page even it’s down.

While in maintenance mode, you may use the ‘secret’ option to specify a maintenance mode bypass token as shown in the example below:

php artisan down --secret="example-secret-token-4b66-afa1-dd72a4c43515"

After placing the application in maintenance mode, you may navigate to the application URL matching this token and Laravel will issue a maintenance mode bypass cookie to your browser:

https://example.com/example-secret-token-4b66-afa1-dd72a4c43515

Closure Dispatch / Chain

Using the new ‘catch’ method, you may now provide a Closure that should be executed if a queued Closure fails to complete successfully after exhausting all of your queue's configured retry attempts.

Dynamic Blade Components

Now you can use Laravel’s in-built ‘dynamic-component’ component to render a component based on runtime value or variable. This feature provides flexibility when you don’t know which component to render until runtime.

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

Event Listener Improvements

Closure based event listeners may now be registered by only passing the Closure to the ‘Event::listen’ method. Laravel will inspect the Closure to determine which type of event the listener handles.

Time Testing Helpers

Laravel's base feature test class now includes helpers that allow you to manipulate the current time:

public function testTimeCanBeManipulated()
{
// 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 explicit time...
$this->travelTo(now()->subHours(6));

// Return back to the present time...
$this->travelBack();
}

Laravel 8 Artisan ‘serve’ Improvements

Previously the ‘php artisan serve’ command had to be manually stopped and restarted every time changes were made in the ‘.env’ file. Now it has been improved with automatic reloading when ‘.env’ file changes are detected.

Tailwind Pagination Views

Now the utility-first CSS framework ‘Tailwind’ has been set as Laravel 8’s default pagination.

Routing Namespace Updates

Prior to Laravel 8, the ‘RouteServiceProvider’ contained a ‘$namespace’ property. Where this property’s value would automatically be prefixed onto controller route definitions and calls to the ‘action’ helper / ‘URL::action’ method. On the other hand Laravel 8.x, this property has been set to ‘null’ as default. Hence, in Laravel 8.x applications, controller route definitions should be defined using standard PHP callable syntax as such:

use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);

Calls to the ‘action’ related methods should use the same callable syntax:

action([UserController::class, 'index']);

return Redirect::action([UserController::class, 'index']);

Furthermore, if you prefer the Laravel 7.x style controller route prefixing, it can be done by simply add the ‘$namespace’ property into the application's ‘RouteServiceProvider’. However, this change only affects new Laravel 8.x applications.

Fonseka Innovations

Laravel 8 update has brought in many powerful functionalities. However, that’s not all it, since the Laravel 8 release, they have been releasing many minor releases.

Combining our expertise in web development and the latest industry-leading technology, we are able to provide high-quality solutions like no other. We are as invested in your products as you are.

We provide many tailored solutions such as:

  • Custom Software
  • Bespoke Websites
  • Web & Mobile Apps
  • Internet of Things
  • Service Integrations
  • eCommerce Solutions

To be an expert in Laravel, continuous improvement is a must for us. Laravel 9 is just around the corner, follow us and learn with us to keep ourselves up to date with awesome techie things.

If you’re looking to create an application built around Laravel, check out Fonseka Innovations for quality and scalable applications.

Laravel Expertss
development testing laravel laravel 8 web website technology tech tech news news trend php