Version 6.0 of Laravel was released on the 3rd of September, continuing the tradition of a new major release approximately every 6 months. This version hints at how the framework is maturing with a range of small but significant feature improvements and the change to semantic versioning. In this article we’ll have a look at these new features that you should be aware of whether you’re upgrading to 6.0 or starting a new Laravel 6.0 project from scratch.
As always, before upgrading a Laravel version, be sure to carefully read and understand the upgrade guides to ensure a smooth upgrade process.
So, let’s see what’s new.
Semantic versioning
Laravel 6 has made the change to semantic versioning, effectively this means the software version must use be in the v.v.v form, which mean respectively major, minor, patch.
- An increment in the major version number means a breaking change or new feature.
- An increment in the minor version number means a non-breaking new feature.
- An increment in the patch version number means a bug fix to an existing feature.
You may notice in your composer.json
of a Laravel 6 project the new caret (^) for the laravel framework compared to a 5.8.x version.
"laravel/framework": "5.8.*",
vs
"laravel/framework": "^6.0",
The current release cycle for Laravel will remain the same.
Job middleware
Laravel now supports specific middleware for Jobs, so where previously you may add code to your Job’s handle()
method, this can be extracted into its own middleware which the Job can then be run through:
use App\Jobs\Middleware\YourMiddleware; // for example, the Job Middleware can go where you choose.
public function middleware()
{
return [new YourMiddleware];
}
Extracting job middleware could be helpful to keep your code DRY if you’re using the same functions in various jobs.
Lazy collections
Lazy collections use PHP’s generators to significantly reduce memory usage when working with large data sets, whether that is a large number of Eloquent models or external files that are utilising Laravel’s collection logic for processing. Lazy collections help by only storing part of the file or on model at a time in memory for processing.
See the Laravel documentation on Lazy Collections for more information.
Improved authorisation responses
A new Gate::inspect()
method has been added which simplifies returning custom error messages to the user for authorisation requests.
Previously, we could use the Gate::allows()
method that only returned a boolean.
if (Gate::allows(‘view’)) {
// the user can view the resource
}
Now using the inspect()
method, if a denied message is provided in the Gate definition, e.g:
Response::deny('You must be a special user to view this resource.');
Then we can allow access or return this message to the user:
$response = Gate::inspect(‘view’, $resource);
if ($response->allowed()) {
// User is authorised to view the resource…
}
if ($response->denied()) {
echo $response->message();
}
Improvements to Eloquent subqueries
There have been a number of improvements to creating Eloquent queries within Eloquent queries, which is very useful when using data from two related tables.
To use an example from the contributor of this feature Jonathan Reinink’s article, we can use the following single query to select all of the destinations and the name of a flight that most recently arrived at that destination:
return Destination::addSelect(['last_flight' => Flight::select('name')
->whereColumn('destination_id', 'destinations.id')
->orderBy('arrived_at', 'desc')
->limit(1)
])->get();
Laravel UI is now a composer package
If you are using some of Laravel’s built in front end scaffolding and bootstrap/vue code, you will now need to use the first party laravel/ui
Composer package. This also means the make:auth
command will no longer create default auth views for login/registration etc. If you wish to still sue these features you will need to run the following command:
composer require laravel/ui --dev
Then to create the auth scaffolding, use:
php artisan ui vue --auth
New website!
Whilst not strictly a Laravel 6 framework update, the Laravel website has been given a new look, with a new logo and layout. Also the Laravel docs pages have been given a new look.
Laravel Vapor
Laravel 6 supports Laravel Vapor, a an auto-scaling serverless deployment platform for Laravel, powered by AWS.
New exception details via Ignition
Ignition is the new default error page for Laravel 6, and should make chasing down bugs easier with some Laravel specific features such as suggested solutions for common problems. Check out the full writeup of Ignition’s features on the author Freek’s website.
That covers off the major new features this version, do check the release notes and when you’re ready to get going, you should be able to start a new Laravel 6 project you now be able to start a new Laravel 6 application with the command line tool:
laravel new my-laravel-6-project
We already have customer projects using Laravel 6 in development and we look forward to launching more soon!