Laravel, a powerful and elegant PHP framework, has quickly become one of the most popular choices among developers for building modern web applications. With its expressive syntax, extensive ecosystem, and focus on best practices, Laravel provides an unparalleled development experience that boosts productivity and promotes code maintainability. However, as with any software, it is crucial to keep Laravel up-to-date to ensure the ongoing security, stability, and performance of your applications.
In September 2020, Laravel 8 was released, bringing an array of exciting new features and improvements. These enhancements make it even more enjoyable to work with and allow developers to build applications more efficiently. While upgrading your Laravel application can be a daunting task, especially if you are working on a large project or have never done it before, it is a critical process to ensure that you continue to benefit from the framework's latest advancements and security patches.
This comprehensive guide aims to help you navigate the Laravel upgrade process, taking you from version 7 (or earlier) to version 8 or above. We will cover essential topics such as preparing for the upgrade, implementing new features, troubleshooting common issues, and establishing best practices for future updates. By the end of this article, you'll be equipped with the knowledge and confidence needed to upgrade your Laravel application and take full advantage of the latest features and improvements.
Preparing for the Upgrade
Before upgrading your Laravel 7 application to Laravel 8 or above, it is crucial to prepare your application to minimize potential issues and ensure a smooth transition. Here are the essential steps for preparing your Laravel 7 application for the upgrade:
- Back up your application: Before making any significant changes to your application, create a backup of your entire project, including the codebase and the database. This will allow you to revert to a working state if something goes wrong during the upgrade process.
- Use version control: If you're not already using a version control system like Git, now is the perfect time to start. It will help you track changes during the upgrade process and make it easier to revert to a previous state if necessary. Create a new branch for the upgrade process, so you can continue working on your main branch without disruptions.
- Update dependencies: Ensure all of your application's dependencies are compatible with Laravel 8. Check the documentation and release notes of each package to verify compatibility. Update any outdated packages to the latest versions that support Laravel 8. Be aware that some packages may introduce breaking changes, so testing is essential.
- Check Laravel's upgrade guide: Laravel provides a comprehensive upgrade guide for each major release, detailing the changes required for a successful upgrade. Review the Laravel 8.x upgrade guide (https://laravel.com/docs/8.x/upgrade) to familiarize yourself with the changes and follow the instructions provided.
- Review Laravel Shift: Laravel Shift (https://laravelshift.com) is a paid service that automates the upgrade process and provides detailed recommendations for updating your codebase. Consider using Laravel Shift to make the upgrade process smoother and reduce manual work.
Setup Lab Environment
composer create-project --prefer-dist laravel/laravel:^7.0 laravel_upgrade cd upgrade_laravel
We have installed Laravel 7 using composer. The project name is upgrade_laravel
.
We then navigate into the new project.
Let's confirm the version installed.
Input
php artisan --version
Output
steve@alila:~/laravel_upgrade$ php artisan --version
Laravel Framework 7.30.6
I am using Laravel 7.30.6.
Here is how to Laravel upgrade to 8 or above.
Perform Laravel upgrade to 8 or above
Update composer.json
Open the composer.json
file and update the following packages
from
"guzzlehttp/guzzle": "^6.3.1|^7.0.1",
"laravel/framework": "^7.29",
"facade/ignition": "^2.0",
"nunomaduro/collision": "^4.3",
"phpunit/phpunit": "^8.5.8|^9.3.3"
to
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.0",
"facade/ignition": "^2.3.6",
"nunomaduro/collision": "^5.0",
"phpunit/phpunit": "^9.0"
Next, check whether the versions of the third-party packages your application uses are supported by Laravel 8. For instance, Laravel ui
should be version ^3.0
.
Update Laravel Installer
Now, update Laravel global installer to a version higher than 4.0
.
composer global remove laravel/installer composer global require laravel/installer
We remove the current global installer and install the latest version.
You can then install Laravel 8 or higher.
laravel new your_app_name cd your_app_name
Migrating to the new Model Factory system
Depending on your application components, it would help to check high and medium changes. High impact changes affect model factories, seeder & factory namespaces, queues, and pagination defaults. For example, unlike Laravel 7.x style factories,
<?php
use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
});
Laravel 8 introduces a new, class-based Model Factory system that replaces the previous, closure-based system. Update your existing model factories by converting them into classes that extend the Illuminate\Database\Eloquent\Factories\Factory
class.
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
public function definition()
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
public function unverified()
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
}
You can continue using Laravel 7 style factories in Laravel 8 after installing the laravel/legacy-factories
package.
composer require laravel/legacy-factories
The medium impact changes affect failed jobs table batch support, maintenance mode updates, the php artisan down --message
option, and the assertExactJson
method.
Lastly, instead of updating each application component one by one, you can avoid the lengthy path by creating a new or latest Laravel project with the target version and then copy-paste your old project's routes and controllers into it.
Conclusion
Upgrading your Laravel application to version 8 or above is crucial for maintaining security, stability, and leveraging the latest features and improvements. The process involves careful preparation, execution of the upgrade steps, and adherence to best practices for future upgrades.
Before upgrading, back up your application, use version control, update dependencies, review Laravel's upgrade guide, and consider using Laravel Shift for assistance. When upgrading, update the Laravel version in composer.json, update packages and dependencies, handle breaking changes, refactor code as necessary, update configuration files, migrate to the new Model Factory system, and optionally implement new Laravel 8 features.
To ensure smooth future upgrades and maintain a healthy codebase, follow best practices such as keeping Laravel and dependencies up-to-date, regularly reviewing and updating code, following Laravel's coding standards, writing tests for your application, and using version control systems.
By following these guidelines, you'll successfully upgrade your Laravel application to version 8 or above, ensuring its ongoing security and performance. Additionally, you'll be better prepared for future upgrades, allowing you to take full advantage of the latest advancements and improvements in the Laravel ecosystem.