Upgrading Laravel Made Easy Using Rector
Upgrading a PHP and Laravel application, along with legacy code can be daunting, especially when transitioning across major versions. However, Rector simplifies the process by automating code upgrades, reducing manual effort, and ensuring compatibility with the latest Laravel features. In this guide, we’ll walk through how to upgrade to Laravel 11 using Rector.
Why Use Rector for Laravel Upgrades?
Traditionally, upgrading Laravel involves:
- Manually updating the
composer.jsonfile - Fixing breaking changes
- Refactoring deprecated code
- Running tests to validate changes
Rector automates most of these tasks by applying predefined upgrade rules, ensuring a smoother transition with minimal effort.
Step-by-Step Guide to Upgrading Laravel Using Rector
1. Update Dependencies
Modify your composer.json file to update Laravel dependencies:
"require": {
"laravel/framework": "^11.0",
"php": "^8.4"
}Then, update dependencies:
composer update2. Install Rector
If Rector is not installed, add it to your project:
composer require rector/rector --devInitialize Rector: (this will generate rector.php)
vendor/bin/rector initThen, install Laravel-specific upgrade rules:
composer require rector/laravel --dev3. Configure Rector for Laravel Upgrades
Modify the rector.php configuration file to include Laravel upgrade sets:
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelLevelSetList;
use RectorLaravel\Set\LaravelSetList;
return RectorConfig::configure()
// For PHP & Laravel
->withPhpSets(php84: true)
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
naming: true,
privatization: true,
typeDeclarations: true,
rectorPreset: true,
)
// For Laravel
->withPaths([
__DIR__ . '/app',
__DIR__ . '/bootstrap',
__DIR__ . '/config',
__DIR__ . '/public',
__DIR__ . '/resources',
__DIR__ . '/routes',
__DIR__ . '/tests',
])
->withSets([
/*
The sets in LaravelSetList only contain changes related to a specific version upgrade.
For example, the rules in LaravelSetList::LARAVEL_110 apply when upgrading to Laravel 11.
*/
LaravelLevelSetList::UP_TO_LARAVEL_110,
/*
Additional Sets, to improve different aspects of your code.
*/
LaravelSetList::LARAVEL_CODE_QUALITY,
LaravelSetList::LARAVEL_COLLECTION,
LaravelSetList::LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALL,
LaravelSetList::LARAVEL_CONTAINER_STRING_TO_FULLY_QUALIFIED_NAME,
LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER,
LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES,
LaravelSetList::LARAVEL_IF_HELPERS,
LaravelSetList::LARAVEL_STATIC_TO_INJECTION
]);
/* TODO: Custom rules
->withRules([
EnsureJsonResponseRector::class
]);
*/4. Check the diff
Running Rector in dry-run mode allows you to preview the changes it will make before applying them. This helps ensure that the transformations align with your expectations and do not introduce unintended modifications.
vendor/bin/rector process --dry-run5. If Everything Looks Good, Run Rector to Upgrade Your Codebase
Execute Rector to apply necessary upgrades:
vendor/bin/rector processThis command scans your project, applies necessary upgrades, and ensures compatibility with Laravel 11.
Conclusion
Using Rector, upgrading Laravel versions becomes a streamlined, automated process. This tool saves time, reduces errors, and ensures best practices are followed when transitioning to Laravel 11. By integrating Rector into your upgrade workflow, you can maintain a modern, efficient Laravel application with minimal hassle.
Get Eljay’s stories in your inbox
Join Medium for free to get updates from this writer.
Our next step is to explore creating custom Rector rules and testing them to further optimize our Laravel upgrade process. Stay tuned for more insights on customizing Rector to fit your project’s specific needs!
Hire Me :)
Are you stuck on your legacy code and don’t want to deal with the nitty-gritty of upgrading? Let me handle it for you!







