This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging

I wrote an article about How Laravel can improve DB query efficiency. After I posted it to the group, some people questioned me and said, “Laravel is the framework he used several years ago. I didn’t realize that people still use it.”

Nani, what do you mean? Don’t forget that PHP is the best language!

In my opinion, Laravel is a very elegant development framework: elegant design patterns, powerful implementation, various convenient extensions, constant version updates, and most importantly, what I consider to be the best technology development community to date.

I have to make a Call for Laravel.

Laravel released version 8.0 on September 8, 2020. Laravel is scheduled to release version 9.0 on January 25, 2022.

Here are some of the new features in Laravel’s latest release (version 8.0) :

Laravel 8 introduces Laravel Jetstream, model factory classes, migration compression, queue batch processing, improved rate limiting, queue improvements, dynamic Blade components, Tailwind paging views, time test assistant, artisan Serve improvements, Improvements to the event listener, along with various other bug fixes and usability improvements, continue to be made to Laravel 7.x.

Laravel Jetstream

Laravel Jetstream is a beautiful application scaffold designed for Laravel. Jetstream provides the perfect starting point for your next project, including login, registration, email verification, two-factor authentication, session management, API support through Laravel Sanctum, and optional team management. Laravel Jetstream replaces and improves on the old authentication UI scaffolding that was available with earlier versions of Laravel. Jetstream is designed with Tailwind CSS, and you can choose between Livewire or a scaffold.

Model class directory

Due to overwhelming community demand, the default Laravel framework now includes an App /Models directory. We hope you like your new home at the Eloquent! All relevant generator commands have been updated, assuming that the model exists in the App /Models directory (if any). If that directory does not exist, the framework will assume that your model should be placed in the App directory.

Model factory class

The model factory class was contributed by Taylor Otwell. The Eloquent model factory has been completely rewritten as a class-based factory, complete with perfect association support. For example, the UserFactory in Laravel looks like this:

<? 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 model. * * @var string */ protected $model = User::class; /** * define the default state of the model. * * @return array */ public function definition() { return [ 'name' => $this->faker->name, 'email' => $this->faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => 'xxxxxxxxx', // password 'remember_token' => Str::random(10), ]; }}Copy the code

Because the new HasFactory trait can be used when generating models, you can use the model factory as follows:

use App\Models\User;

User::factory()->count(50)->create();
Copy the code

Because the model factory is now a simple PHP class, the state transition can be written directly as a method. You can also add as many classes as you want to the Eloquent factory. For example, if your User model has a suspended property and you want to change one of its default property values, you can do so using the base factory class’s state method. The method name can be arbitrarily set, since this is a typical PHP method.

/** * indicates that the user is disabled ** @return Illuminate\Database\Eloquent\Factories\Factory */ public function suspended() {return $this->state([ 'account_status' => 'suspended', ]); }Copy the code

Once defined, we can use it like this:

use App\Models\User;

User::factory()->count(5)->suspended()->create();
Copy the code

As mentioned earlier, Laravel 8’s model factory includes perfect support for model correlation. Now, assuming that our User model has a posts correlation method, we just need to execute the following code to generate a User with three posts.

$users = User::factory()
            ->hasPosts(3, [
                'published' => false,
            ])
            ->create();
Copy the code

To simplify the upgrade process, we have released a Laravel/Legacy-Factories extension that supports the previous model factories in Laravel 8.

The new version of the model factory also includes many features, so check out my updated documentation for more.

Study group please click here, study together, make progress together!!