Laravel 8 was released on September 8th, 2020. This release continues the improvements made in the previous release (version 7), as well as new features that include support for Jetstream, job batching, dynamic blade component, model factory classes, improved artisan serve, and many others.
In the course of this article, we will take a look at 13 new features introduced in this new release as listed below:
Laravel Jetstream is a beautifully crafted application for scaffolding Laravel applications. Jetstream, which was designed using Tailwind CSS, provides a perfect starting point for new projects with features such as authentication, profile management, security, and API support using Laravel Sanctum.
Also, Jetstream offers two options for frontend scaffolding with either Livewire and Inertia.
Laravel Livewire — is a library that makes it possible to build full-stack applications on Laravel without the need to pull in other frontend libraries/frameworks such as React and Vue.js. Since Livewire uses the already familiar blend templating engine, Laravel developers can easily build dynamic interfaces without leaving the comfort of Laravel.
Inertia.js — is a package bundled with Laravel Jetstream which lets you quickly build client-side templates with Vue.js. What makes this cool is you get to enjoy the full power of Vue without the complexity of frontend routing because you get to use the standard Laravel router you’re familiar with.
**Jetstream installation — **if you have the Laravel installer installed, you easily install Jetstream with your Laravel installation by adding the --jet
flag like this
$ laravel new project-name --jet
Complete the setup by running migrations:
$ php artisan migrate
Alternatively, you can use composer to install Jetstream into a new Laravel application. Installing Jetstream via composer will require you to run the jetstream:install
artisan command which accepts the name of your preferred frontend stack e.g livewire or Inertia.js. This is can be done by running the commands below:
$ php artisan jetstream:install livewire
$ php artisan migrate
$ npm install && npm run dev
You can visit the Jetstream official documentation to learn more.
There have always been suggestions that Laravel should have the Model
directory as the default for storing models. In 2016 Taylor Otwell made a poll about it and the results showed a higher percentage of people want a default model directory. Four years later and the people’s request has been granted.
In the previous versions of Laravel, all model files were stored in the /app
directory by default if you didn’t specify a path when generating a model. However, since the new update, Laravel now includes the app/Models
directory by default.
So, when you run the $ php artisan make:model ModelName
command, ModelName.php
will be saved in the app/Models
. However, if the directory doesn’t exist, Laravel will assume the application models are already in the app/
directory.
Eloquent model factories let us define patterns used in generating fake data when testing our application. In previous versions, Laravel provides a $factory
global object which we can extend to define our factories. Starting in Laravel 8, factories are now class-based with improved support for relationships between factories (i.e. a user has many posts).
Defining a factory previously looks something like this:
// database/factories/UserFactory.php
use Faker\Generator as Faker;
use Illuminate\Support\Str;
$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
});
We can then use the defined factory like so:
public function testDatabase()
{
$user = factory(App\User::class)->make();
// Use model in tests...
}
Since the new version, factory will now be defined as a class, like this:
// database/factories/UserFactory.php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
}
With the new HasFactory
trait available on generated models, the model factory may be used like so:
use App\Models\User;
public function testDatabase()
{
$user = User::factory()->make();
// Use model in tests...
}
Say goodbye to bloated migration folders with the new migration squashing feature which lets you squash large migration files into one SQL file. The generated file will be executed first when you run migrations, Laravel then executes any other migration files that are not part of the squashed schema file. You can squash your migration files using the artisan command below:
$ php artisan schema:dump
// Dump the current database schema and prune all existing migrations...
$ php artisan schema:dump --prune
When you run the command Laravel will write a schema file to your database/schema
directory.
The new release of Laravel also comes with a nifty feature that allows you to dispatch a group of jobs to be executed in parallel. To monitor the progress of grouped/batched jobs, you can use the then
, catch
, and finally
methods to define completion callbacks like this:
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)),
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();
return $batch->id;
You can check out the Laravel documentation to learn more about the new job batching feature.
#laravel #php #web-development #programming #developer