What's New in Laravel 8

Laravel 8 - New Features

Laravel framework version 8, was released on September 8. 2020. Laravel 8 comes with exciting new features and a guide to upgrade.

Table of contents

  1. Laravel Jetstream
  2. How to use Laravel Jetstream
  3. Models Updates
  4. Models directory is back!
  5. Model factory classes
  6. Migration Squashing
  7. Job Batching
  8. Improved Rate Limiting
  9. Schema Dumping
  10. Closure Dispatch / Chain catch
  11. Dynamic Blade Components
  12. Maintenance Mode

Laravel Jetstream

Laravel Jetstream is a designed application scaffolding for Laravel. Jetstream provides the perfect starting point for your next project and includes login, registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management. Laravel Jetstream replaces and improves upon the legacy authentication UI scaffolding available for previous versions of Laravel.

Jetstream is designed using Tailwind CSS and offers your choice of Livewire or Inertia scaffolding.

Jetstream provides a great built-in options:

  • Login and registration
  • Email verification
  • Two-factor authentication (2FA)
  • Session management
  • Laravel Sanctum API support
  • Designed with Tailwind CSS
  • Two versions for handling frontend: Livewire (TALL stack) or Inertia.js (Vue.js)

How to use Laravel Jetstream

You can create a new application with Jetstream using the Laravel installer.

Run the following:

laravel new your-project --jet

Then choose a which stack you want to use: Livewire or Inertia. Then you need to run your database migrations with:

php artisan migrate

See your application at http://localhost:8000 by running:

php artisan serve

Models Updates

Models directory is back!

The app/Models directory is back! Some time ago when Laravel 5 launched, developers noticed the Models directory was missing. Instead, new models were created directly in the app directory.

With Laravel 8, the beloved app/Models directory has been restored. If you prefer the old structure, you can always modify your codebase to remove the Models directory again.

Model factory classes

Model factories are class based factories that add a lot of new features to Laravel 8. For each model there’s also a factory class, where there is a definition method that says which attributes it will generate for that model. Your models make use of that factory through the Factory trait.

Laravel 8 Factory

Migration Squashing

Laravel 8 introduces a new feature: migration squashing

As you build your application, you may accumulate more and more migrations over time. This can lead to your migration directory becoming bloated with potentially hundreds of migrations. If you're using MySQL or PostgreSQL, you may now "squash" your migrations into a single SQL file with following commands:

php artisan schema:dump
 php artisan schema:dump --prune

Job Batching

Laravel's job batching feature allows you to easily execute a batch of jobs and then perform some action when the batch of jobs has completed executing.

To use the new batching feature, first define your job as you normally would. The example below has been simplified to show the new Batchable trait.

 namespace App\Jobs;

 use Illuminate\Bus\Batchable;

 class SendEmail implements ShouldQueue
   use Batchable;

     * Execute the job.
     * @return void
   public function handle()
     if ($this->batch()->cancelled()) {
       // Detected cancelled batch...

     // Batched job executing...

Once you have your job defined, you can dispatch a batch of jobs in your controller using the batch method of the Bus facade.

  use App\Jobs\SendEmail;
  use App\User;
  use Illuminate\Bus\Batch;
  use Illuminate\Support\Facades\Batch;
  use Throwable;

  $batch = Bus::batch([
    new SendEmail(User::find(1)),
    new SendEmail(User::find(2)),
    new SendEmail(User::find(3)),
  ])->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...

  return $batch->id;

You'll also notice the addition of the then, catch, and finally methods. You can use these to define completion callbacks for your batch of jobs.

Improved Rate Limiting

Laravel 8 there is a new way to create custom Rate Limiters. We can define our custom Rate Limiter in any Service Provider typically it should be in RouteServiceProvider like so.

Rate limiters are defined using the RateLimiter facade's for method. The for method accepts a rate limiter name and a Closure that returns the limit configuration that should apply to routes that are assigned this rate limiter:

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

 RateLimiter::for('testing', function (Request $request) {
     return Limit::perMinute(1000);

Now we can attach testing Rate Limiter using it's name with throttle middleware like throttle:testing instead of throttle:60,1:

 Route::middleware('throttle:testing')->get('/user', function(){

Schema Dumping

Schema dumping is a way to squash migrations to a single file. It generates a schema file that has the whole schema for your database in a SQL form. This is used during development, useful for integrating new developers on your project that already has a large number of migrations. It supports MySQL, Postgres, SQLite.

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

Closure Dispatch / Chain catch

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:

 use Throwable;

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

Dynamic Blade Components

Sometimes you may need to render a component but not know which component should be rendered until runtime. In this situation, you may now use Laravel's built-in dynamic-component component to render the component based on a runtime value or variable.

Maintenance Mode

Laravel 8 also brings some improvements to maintenance mode. Maintenance mode is a really helpful feature that allows you to "disable" your application while you're making updates to it.

With Laravel 8, you no longer need to allow certain IPs explicitly. Instead, you can use the secret option to create a maintenance mode bypass token:

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

Now you can access your application while in maintenance mode by appending the token to your application's URL, e.g. https://mysite.com/1630542a-246b-4b66-afa1-dd72a4c43515.

The bypass cookie will be saved to your browser, and you can continue navigating your application from its normal URL, e.g. https://mysite.com.

If you like this blog post and it was useful to you, please follow us on Twitter and Facebook.

Related articles


Post a Comment