What’s new in Laravel 7?

Tram Ho

Although Laravel 6.0 was launched not long ago (September 2019), laravel today has released its 7th version with quite a lot of big and useful updates: introduced Laravel Airlock, improved routing speed, depending on adjust Eloquent casts, Blade component tags, fluent string operations, HTTP client, first-party CORS support, improve scoping for route model binding, customize stub, improve database queues, support multiple mail drivers, casts when accessing test , new command test , fix bugs and improve many other parts. Here are the details:

Laravel Airlock

Laravel Airlock provides a lightweight authentication system for SPAs (single page applications), mobile applications, and more simply token based APIs. Airlock allows each user of your application to create multiple API tokens for their account. These tokens can be granted abilities / scopes specifying what actions it can take.

For detailed information about Laravel Airlock, see details in the Airlock documentation .

Customize Eloquent Casts

Laravel has built in many useful cast types. However, sometimes you need to define different cast types for your project. You can now do that by creating a class implements CastsAttributes interface.

These implements interface classes must define two methods, get and set . The get method will convert the value from the DB into the desired data type. In contrast, the set method converts the value of the field into a value that can be stored in the database. Example of json cast built-in:

After defining the cast type, you can assign properties to the Model through the class name.

For details, see the Eloquent documentation .

Blade Component Tags & Improvements

Blade components have been improved, allowing the use of tags, attribute management, component classes, inline view components, etc. Because these changes are very wide, please visit the full Blade component documentation to learn how. the most complete.

In short, from now on, a component can be assigned to a data class of its own. All properties and public methods of the Class will be available in component view. Any other HTML attributes of the component will be stored in the $attribute variable.

For example, we create an App View Components Alert component as follows:

And the template of the above component is as follows:

Meanwhile, this component can be used in other Blade templates by using the component tag:

As mentioned, this is only a very small example of many changes of Laravel 7. Blade. Please refer to the full Blade component documentation for details.

HTTP Client

Now, Laravel provides a minimal HTTP Client using the Guzzle HTTP client , allowing you to make HTTP requests as quickly as possible, focusing on common cases and developers’ experience. Example of sending POST request with JSON data:

In addition, the HTTP Client provides a handy test function:

To learn more about the HTTP client, please visit the HTTP client documentation .

Fluent String Operations

As you know, Class Illuminate Support Str provides quite a lot of useful functions for manipulating strings. Laravel 7 provides a more object-oriented library for working with strings, based on existing functions. You can create an Illuminate Support Stringable object using the Str :: of method. Meanwhile, many methods can be used in succession to manipulate the string:

To learn more and the available methods, please visit the full documentation .

Improve Route Model Binding

Key Customization

Sometimes you may want to resolve Eloquent models with a column other than id. To do that, Laravel 7 allows you to specify the column in the definition parameter definition:

Automatic Scoping

Sometimes, when binding multiple Eloquent models implicitly in the same route, you want to bind the second Eloquent model to that of the first Eloquent model. For example, when you want to access a slug post for a specified user:

When using nested route parameters, Laravel 7 automatically limits the query to get nested model information according to its parent, using conventions to guess the relationship in the parent model. In this case, suppose the User model has a posts relationship (plural of parameters in the route), which will be used to retrieve Post model information. To see more about route model binding, please visit the routing documentation .

Multiple Mail Drivers

Laravel 7 allows configuring multiple “mailers” for an application. Each mailer configuration in the mail configuration has its own options, including “transport,” which allows your application to use different email services to send email messages. For example, your application can use Postmark to send email directly, and use Amazon SES to send email in bulk.

By default, Laravel uses the mailer which is set by default in the mail config file. However, you can use the mailer method to send email using the specified mailer:

Improve Route Caching speed

Laravel 7 includes a new matching compiled method, cached routes are cached with the Artisan route:cache command. For large applications (about 800 routes or more), these improvements result in 2 times faster. These changes will automatically be applied without changes from your system.

CORS Support

Laravel 7 includes first-party support to configure Cross-Origin Resource Sharing (CORS) OPTIONS by integrating the Laravel CORS package written by Barry, for example. Heuvel. The default cors configuration file will be added in the Laravel application.

To learn more about CORS support in Laravel 7.x, please visit the CORS documentation .

Query Time Casts

Sometimes, you need to apply cast when executing a query, such as select raw from the table. For example:

The last_posted_at attribute in the result of the above query is a string. It is more convenient to cast this attribute to date when executing the query. To do this, we use the withCasts method provided by Laravel 7:

Improved MySQL 8+ Database Queue

In previous releases of Laravel, the database queue was not powerful enough to be used in production. However, Laravel 7 provides enhancements for applications using MySQL 8+. By using the FOR UPDATE SKIP LOCKED clause and other SQL enhancements, the database driver can now be used safely in production applications.

Artisan test command

In addition to the phpunit command, you can now use the Artisan test command to run tests. The Artisan test runner provides a nicer interface and displays more information about the running test. In addition, the runner will automatically stop when the first failure occurs:

Any parameters that can be passed to the phpunit command can also be used with the php artisan test command:

Improved Markdown Mail Template

The default maildown template has been refreshed, more modern based on the Tailwind CSS color palette. Of course, this template can be published and customized according to your application needs:! [] ( Https://images.viblo.asia/a9cdb040-fbe7-446f-9427-16e5925b5290.p

For more information about Markdown mail, please visit the mail documentation .

Customizable Stub

The Artisan make command is used to create many classes, such as controllers, jobs, migrations, and tests. These classes are derived from “stub” files and are based on the value you enter. However, sometimes you may want to make minor changes to the file created by the Artisan command. To do this, Laravel 7 provides the stub:publish command to publish most regular stubs for you to customize:

php artisan stub:publish

The published stub will be located in the stubs directory in the root of the application. Any changes you make to these stubs will be reflected when you create the corresponding class with the Artisan make command.

Queue maxExceptions Configuration

Sometimes you want to specify a job that can be tried again and again, but it will fail if there are a number of exceptions when trying again. In Laravel 7, you can define the maxExceptions attribute in the job class:

In this example, the job is released for ten seconds if the application is unable to obtain a Redis lock and will continue to be retried up to 25 times. However, the job will fail if three unhandled exceptions are thrown by the job.

Reference: Laravel Release Notes

Share the news now

Source : Viblo