Released in February ’19, Laravel 5.8, the latest version of the most popular PHP framework, brought great new updates while continuing the improvements made in the previous releases. These include:
- Improved email validation
- dotenv 3.0
- Automatic policy resolution
- “HasOneThrough” relationship
- Token guard token hashing
- Artisan command improvements
- Artisan serve improvements
- Artisan call improvements
- Mock testing helper methods
- Higher-order “were” eloquent method
- Default timezone scheduler
- Cache TTL
- Blade file mapping
- Added support for libraries
- Carbon 2.0 Support
- Postmark driver
- Beanstalk 4.0 support
In this tutorial, we will discuss all these important changes & new features in greater depth.
Improved email validation
Email validation logic has been improved in this latest update. The email addresses such as [email protected] will pass as valid because it now supports international characters’ addresses.
dotenv 3.0
This new version comes with support for the dotenv 3.0. If you don’t know about Dotenv, it also helps users manage their project’s .env environment file.
The dotenv’s 3.0 key new features are supported for multiline strings and white space at the end of strings in your .env file. To know more about dotenv, you can pay a visit to its GitHub page.
Automatic policy resolution
This popular PHP framework handles user authorization through two primary ways, one of which is policies. Model policies needed to be registered in the AuthServiceProvider in the older versions. You were required to manually register each policy, which is no longer the case; now, Laravel can auto-discover policies as long as the naming and location of policies were according to standard.
“HasOneThrough” relationship
HasOneThrough, a new eloquent relationship, has been introduced by this new version. Although new in Laravel, it has already been a part of other frameworks. It may be a little hard to understand but let’s try it through this example, say we have three tables: a users table, a suppliers table, and a history table. A user is related to a supplier, and a supplier has one history.
Previously to get a user’s history, you will have to find the user then write something like $user->suppliers->history. You may use a has one through relationship to skip this step, accessing a user’s account history straight away like this: $history = $user->history through the supplier model.
Token guard token hashing
Not many know this about Laravel API authentication, but you are not always required to use Laravel Passport. A simpler token guard provides basic API authentication. This new version now supports storing tokens using the secure hash algorithm SHA-256, which is more secure than just storing plain-text tokens.
Artisan command improvements
Following are the 2 major improvements to the Laravel CLI (artisan):
Artisan serve improvements
Previously, the PHP artisan serve command could only serve on port 8000 by default; if you try to serve another application with the same command, it will fail. This new version makes it possible to serve multiple Laravel applications at once by utilizing all available ports up to port 8009.
Artisan call improvements
In the older versions, the artisan commands were passed as an array in the second argument to the artisan call function, but now this new version makes it possible to pass the entire command as the first argument.
Mock testing helper methods
This new version added two new functions, “mock” and “spy”, which will help make mocking objects simpler. These functions automatically inject the mocked class into the service container, making the test code cleaner and readable.
“Higher-order” or where the eloquent method
In the older versions, we can combine the model scopes using the “or” query operator, which requires a closure callback. At the same time, this new version introduces a “higher-order” orWhere method, which does not require the use of a closure callback.
Default timezone scheduler
In Laravel, one could specify what time and timezone a task or cron job will run using the timezone method. However, previously you had to specify time zones for every task in the application, which would make it very cumbersome, especially if there were many of them. This new version makes it possible to define the schedule timezone function, which will return the default timezone for all scheduled tasks.
Cache TTL
Previously, caching was set in minutes. This has changed in this new version to seconds to give users more granular control over cache duration and compliance with the PSR-16 caching library standard. So in any reference to cache in your application, remember to update to seconds.
Blade file mapping
In this new version, Laravel adds a comment at the top of every Blade file, which contains a path to the original Blade template file. This feature provides an opportunity for Blade debugging in the PhpStorm IDE by JetBrains.
Added support for libraries
Laravel 5.8 comes with added support for the following libraries:
Carbon 2.0 support
Carbon is a package that extends PHP’s own DateTime class and makes it very easy to work with dates and time. This new version provides support for version 2.0 of Carbon. This removes the extra step of including Carbon in multiple Laravel applications individually.
Postmark driver
This new version makes it possible to utilize the Postmark mail driver (a quick and reliable transactional email service).
Beanstalk 4.0 support
This new version provides support for release 4.0 of the Pheanstalk queue library. If you are using the Pheanstalk library in your application, please upgrade your library to the 4.0 release via Composer.
Conclusion
As stated earlier, this new version brought great new updates while continuing the improvements made in the previous releases. In this article, we have pretty much covered all the notable changes coming in Laravel 5.8. However, this is not all as there are still many things waiting to be discovered; you can find them out for yourself by visiting the Laravel website.