What’s New in Laravel 8?

Laravel 8 is now officially released and has some brand new features such as Laravel Jetstream, new application scaffolding, class-based migration factories, migration squashing, rate-limiting improvements, time testing, dynamic components, and many more.

Support Policy: Laravel 8 will continue to receive bug fixes up to March 8th, 2021, and security fixes until September 8th, 2021 next year.

New Laravel Installer

Before we get started, Laravel released a new version of “Laravel/installer” which includes the ability to quickstart new Laravel jetstream projects. So, let’s install this globally through the composer.

# uninstall existing version (if you have one)
composer global remove laravel/installer

# reinstall new one
composer global require laravel/installer

Laravel Jetstream

Laravel Jetstream is an improvement upon existing Laravel scaffolding in previous versions. Jetstream ships with a new dashboard page along with login, registration, email verification, two-factor authentication, session management, sanctum, and optional team management.

it is designed with Tailwind CSS and gives the user a choice between Livewire and inertia.

To start a project with Jetstream:

laravel new laravel-test --jet --stack=livewire

npm install && npm run dev

php artisan serve

New Model Directory

Laravel 8 now has “app/Models” directory for all newly generated models. However, in case if the “app/Models” folders don’t exist then the framework will fallback to previously used “app/" directory.

Class-Based Model Factories

In Laravel 8, the framework has switched to class-based factories instead of factory functions along with improved relationship support.

So, if you look at the User model, there’s a new hasFactory trait that allows you to directly generate data off of the model.

use App\Models\User;/**

 * Run the database seeders.
 *
 * @return void
 */
public function run()
{
    User::factory()->count(20)->create();
}

Migration Squashing

When your Laravel application starts to have a large number of migrations, you can now squash” your migrations into a single SQL file.

You can execute the following command to get started.

php artisan schema:dump

// Dump the current database schema and prune all existing migrations...
php artisan schema:dump --prune

After executing the dump command, Laravel will create a “schema” file in your “database/schema” folder. Now, when you attempt to migrate then your schema file will have priority over the migration file.

Job Batching

New job batching allows you to seamlessly execute the batch of jobs that goes together and do something once all the jobs have completed executing. This enables further control processing and handling of dispatched jobs.

use App\Jobs\JobA;
use App\Jobs\JobB;

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

$batch = Bus::batch([
    new JobA(),
    new JobB(),
])->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;

Improved Rate Limiting

Laravel 8 provides a more flexible way to define rate-limiting while maintaining compatibility with previous versions.

Now rate limiting is defined using RateLimiter a facade. The first parameter in for() method accepts the name of the rate limiter and second parameter is a closure that returns the limit configurations that should be applied to the routes.

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

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

Maintenance Mode

In the previous version of Laravel, PHP artisan down a command would set the application to maintenance mode, and only a defined allowed list of IP addresses were able to access the application.

Now, the feature is improved upon the previous implementation by replacing the IP address with the secret token.

php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"

// then navigatge to this application url with token to gain access
https://example.com/1630542a-246b-4b66-afa1-dd72a4c43515

Closure-Based Event Listeners

Handling closure-based events can be achieved using a new catch method. If a queued closure fails to complete successfully, you can perform extra operations to debug your application now.

use Throwable;

dispatch(function () use ($podcast) {
    $podcast->publish();
})->catch(function (Throwable $e) {
    // This job has failed...
});

Time Testing Helpers

New-time testing helpers will allow you to modify the timestamp returned by now() or Illuminate\Support\Carbon::now(). This update includes a test class to help you manipulate the current time while executing 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 explicit time...
    $this->travelTo(now()->subHours(6));

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

Dynamic Blade Components

If you need to render a blade component dynamically at runtime, there’s a new <x-dynamic-component/> tag to render components.

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

Other Changes

php artisan serve will automatically reload when environment variables are changed in your .env file. Whereas, previously you needed to manually restart.

Tailwind CSS styling will be used as a default for pagination views.

Asmit Nepali, a Full Stack Developer, holds a Software Engineering degree from Gandaki College of Engineering and Science. Proficient in PHP Laravel, Vue.js, MySQL, Tailwind, Figma, and Git, he possesses a robust technical skill set.

Leave a Comment