Website development

We are Makklays. We help to realize the idea.

What's new in Laravel 8?


Makklays - Articles image

eye 17

September 2020 has come, and with it the release of Laravel 8 (Laravel 8). Without changing its traditions, every six months (since Laravel 6) the Laravel team releases a new version of Laravel with updates and improvements. Laravel Non-LTS (general release), Laravel 8 will provide 6 month bug fixes until March 8, 2021, and 1 year security patches until September 8, 2021. So let's take a look at all the new improvements what's new in Laravel 8

What's new in Laravel 8



Laravel 8 has several new features. Butch function for grouping jobs, improving previous functions like route caching, bug fixing. Also, changing the default model directory, Schema Dump, Laravel Jetstream, site maintenance mode, Rate Limit, model factory classes, compressing migrations, speed limiting improvements, timing helpers, dynamic blade components and many more features ... Let's take a look at What's New and Improved in Laravel 8 . Highlighted the most interesting improvements.

Laravel Jetstream



Laravel Jetstream is an enhancement to Laravel UI. New login forms including login, registration, email verification, two-factor authentication, session management, API support via Laravel, and team management.

Models folder



The subfolder is now located in app / Models and, accordingly, all commands believe that the model files are located in the app / Models directory. But if you do not have the app / Models directory, the models will be searched in the app directory (location from previous versions).

Dynamic Blade Components



Laravel 8 introduces components for dynamic rendering. This comes in very handy when you need to dynamically render the blade at runtime.



Dispatch / Chain Closure



Using the new catch method, you can now provide a closure to be executed if the queue closure cannot be completed successfully after exhausting all configured retry attempts on your queue:

  use Throwable;

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


Artisan serve



Artisan Serve now automatically reloads environment settings when it detects changes to environment variables in your local .env file. Previously, this command had to be manually restarted.

Routing Namespace Updates - Route



In previous releases of Laravel, the RouteServiceProvider object contained a $ namespace property. The value of this property will be automatically added to controller route definitions and helper method action calls. In Laravel 8.x, this is the default property. This means Laravel will not automatically prefix the namespace. Therefore, in newer Laravel 8.x applications, controller route definitions must be defined using the standard PHP callable syntax: URL :: action null

  use App \ Http \ Controllers \ UserController;

Route :: get ('/ users', [UserController :: class, 'index']);

Calling the action of related methods must use the same calling syntax:

  action ([UserController :: class, 'index']);

return Redirect :: action ([UserController :: class, 'index']);

If you prefer the Laravel 7.x style controller route prefix, you can simply add the $ namespace property to your RouteServiceProvider application.

Bus Batching



Laravel's batch processing feature allows you to easily execute a batch of jobs and then do some action when the batch has finished.

The new Bus batch () facade method can be used to submit a batch of jobs. Of course, grouping is primarily useful in conjunction with completion callbacks. That is, using the then, catch, and finally methods to determine if the batch callback is complete. Each of these callbacks will get an instance when called: Illuminate \ Bus \ Batch

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

$ batch = Bus :: batch ([
new ClassFun (Fun :: find (1)),
new ClassFun (Fun :: find (2)),
new ClassFun (Fun :: find (3)),
new ClassFun (Fun :: find (4)),
new ClassFun (Fun :: 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;


Improving Service State



In previous versions of Laravel php artisan down, the maintenance feature could be bypassed by using an “allowed list” of IP addresses that were allowed to access the application. This feature has been removed in favor of the simpler “secret” solution.

While in maintenance, you can use the secret option to specify the maintenance state bypass token:

  php artisan down --secret = "12345a-321a-3b43-12b1-aab212-1017828"

After putting your application into service state, you can navigate to the site URL that matches the specified token and Laravel 8 will give your browser a service state bypass cookie:

https://ayoursite.com/12345a-321a-3b43-12b1-aab212-1017828

Model Factory Classes



Since Laravel 8, Eloquent model factories are class-based with improved support for factory relationships (i.e. the user has a lot of messages). I think you'll appreciate how good the new syntax is for creating records using the new and improved model factories:

  use App \ Models \ User;

User :: factory () -> count (50) -> create ();

// using a model state "suspended" defined within the factory class
User :: factory () -> count (5) -> suspended () -> create ();


Compress Migration



In cases where your site contains many migration files, you can compress them into a single SQL file. This file will be executed first when the migration starts, and then any remaining migration files that were not included in the compressed file. Compressing existing migrations can reduce the number of migration files and also improve performance when running tests in Laravel 8.

taylorotwell (developer Laravel) writes about it like this (translation):
This PR adds support for the new php artisan schema: dump command, which uses mysqldump or pgdump to dump the current state of your schema to database / schema / {connection} -schema.mysql.

When this file exists and php artisan migrate or php artisan migrate: fresh is running AND no migrations have been performed for the database yet (migration table is empty), this schema file will first be loaded into the database, and then all pending migrations will be run. This means that in fact this schema file will usually only be used during local development or during CI testing. In a production environment, you usually already have migrations that have run in the past, so this schema file will never run.

After the schema file is created, any or all existing migrations can be removed if they have already been run in your production environment. They are no longer needed during local development because the schema file exists. For convenience, the --prune flag has been added to schema: dump to remove existing migrations after dumping the schema.

This new feature solves two problems. First, it saves developers from having to have a huge migration directory full of files they no longer need. Second, loading a single schema file is faster than running hundreds of migrations for each test class in your applications, so your tests can run much faster when using a schema.

Eloquent with ()



The developer of this novelty ryancco writes about it like this:
Provides support for an alternative, more elegant syntax for constrained eager load relationships in Laravel 8.

Not to make any breaking changes, it still supports all currently possible method calls:

  single line (-> with ('foo'))
list of strings (-> with ('foo', 'bar'))
[non-associative] array of strings (-> with (['foo', 'bar']))
associative array of string keys and closure of values ​​(-> with (['foo' => function () {}]))

This will introduce support for closures as the second argument, as shown below:

  // single relationship
$ query-> with ('foo', function ($ query) {
$ query-> where ('status', 'active');
});

// multi relationship
$ query-> with ('foo', function ($ query) {
$ query-> where ('status', 'active');
}) -> with ('bar', function ($ query) {
$ query-> where (...);
});

An alternative and current syntax for this could be to use an associative array as shown below:

  // single relationship
$ query-> with ([
'foo' => function ($ query) {
$ query-> where ('status', 'active');
},
]);

// multi relationship
$ query-> with ([
'foo' => function ($ query) {
$ query-> where ('status', 'active');
},
'bar' => function ($ query) {
$ query-> where (...);
}
]);

It may be easier to read, write and reason about. More importantly, it follows a convention already seen in many other methods in the framework and in the Builder class, such as the Builder::where method, which can take either an array or a separate set of columns, operands, and values.