This is the third 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.

The Laravel Jetstream and model factory classes were introduced in the last article, so you can see how anyone can question me about still developing with Laravel? Don’t forget that PHP is the best language. (1)

The migration of compression

As you develop your application, your migration files may accumulate over time, which can cause your migration directory to become very bloated. Now you can compress your migration file into an SQL file. Schema :dump

PHP artisan schema:dump // Dump the current schema and delete all existing migrations... php artisan schema:dump --pruneCopy the code

After executing this command, Laravel will write a “schema” file to the database/schema directory. When you migrate the database without performing any other migrations, Laravel will first execute the SQL in the schema file and then perform the remaining migrations not included in the schema.

Task batch processing

Laravel’s task batching feature allows you to simply execute a batch of tasks and then perform some actions after the batch has completed. A batch method has been added to the Bus facade to execute batch tasks. Of course, batch processing is primarily used in conjunction with callbacks. So, you might need to use then, catch, finally methods to define the full callback. Each of these callbacks receives a Illuminate, Bus, and Batch instance when called:

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;
Copy the code

Rate limiting optimization

Laravel’s request rate limiter has been enhanced to provide greater flexibility and functionality, while being compatible with the previous version of Throttle middleware. Use the for method of the RateLimiter facade to define a RateLimiter. The first argument to the for method is the name of the rate limiter, and the second argument is a closure function that returns the configuration of the rate limiter.

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

RateLimiter::for('global', function (Request $request) {
    return Limit::perMinute(1000);
});
Copy the code

Because the rate limiter callback is passed in an HTTP request instance, you can dynamically set the rate limit based on the request or the currently authenticated user.

RateLimiter::for('uploads', function (Request $request) {
    return $request->user()->vipCustomer()
                ? Limit::none()
                : Limit::perMinute(100);
});
Copy the code

Sometimes you may want to limit the rate based on certain values. For example, if you want to limit users to 100 requests per minute per IP address, you can do this using the BY method:

RateLimiter::for('uploads', function (Request $request) {
    return $request->user()->vipCustomer()
                ? Limit::none()
                : Limit::perMinute(100)->by($request->ip());
});
Copy the code

Use throttle middleware to bind the rate limiter you just created to a route or route group. Pass the name of the rate limiter to the middleware for binding:

Route::middleware(['throttle:uploads'])->group(function () {
    Route::post('/audio', function () {
        //
    });

    Route::post('/video', function () {
        //
    });
});
Copy the code

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