Laravel 5.8旧项目升级至6/7/8失败,求升级步骤指导
Hey there, let's work through your Laravel 5.8 upgrade struggles together. I've reviewed your composer.json and can break down exactly what you need to do—no guesswork involved.
First: Should You Delete Any composer.json Config Items?
Short answer: Not necessarily delete, but update or adjust incompatible configs and dependencies. Blindly removing items will cause more issues. Let's break down what in your file needs tweaking:
1. Dependency Version Mismatches
Your current dependencies are locked to Laravel 5.8. Each newer Laravel version has strict requirements for supporting packages. For example:
laravelcollective/html: 5.3.x only works with 5.8; for Laravel 6/7/8 you need 6.0/7.0/^8.0 respectively.phpunit/phpunit: ~6.0 won't work with Laravel 6+ (needs ^7.0 for 6.x, ^8.0 for 7.x, ^9.0 for 8.x).fzaninotto/faker: This package has been renamed tofakerphp/fakerfor Laravel 6+, so you'll need to replace that.
2. Outdated Scripts
Your post-install-cmd and post-update-cmd include php artisan optimize—this command was removed in Laravel 6. You'll need to remove that line entirely (or replace it with php artisan config:cache/php artisan route:cache if you want auto-caching, though it's not recommended for dev environments).
3. Autoload Quirk
I noticed a typo in your PSR-4 autoload entry: "Weltwoche\": "app/" should be "Weltwoche\\": "app/" (missing a backslash escape). Fix this first—it'll prevent autoloading errors during upgrades.
4. Unmaintained Packages
chumper/zipper: This package hasn't been updated in years and may not support newer Laravel versions. Replace it with league/ziparchive or use Laravel's built-in Storage class for zip operations (Laravel 8+ has native zip support).
Feasible Upgrade Methods for Old Laravel Projects
The biggest mistake people make is trying to jump directly from 5.8 to 8.x. Step-by-step incremental upgrades (5.8 → 6.x → 7.x → 8.x) are far more manageable—each version has fewer breaking changes to address at once.
Step 1: Pre-Upgrade Prep
- Backup everything: Commit your current code to Git, export your database, and make a full project backup.
- Update PHP: Ensure your server meets the target Laravel version's PHP requirement:
- Laravel 6: PHP 7.2.5+
- Laravel 7: PHP 7.3+
- Laravel 8: PHP 7.3+ or 8.0+
- Clear Composer cache: Run
composer clear-cacheto avoid old version conflicts.
Step 2: Upgrade to Laravel 6.x
Modify your composer.json with these changes:
{ "require": { "php": "^7.2.5", "aporat/store-receipt-validator": "dev-master", // Check if this has a stable release for 6.x "barryvdh/laravel-debugbar": "^3.2", "league/ziparchive": "^3.0", // Replace chumper/zipper "doctrine/cache": "^1.10", "doctrine/dbal": "^2.10", "google/apiclient": "^2.12", "intervention/image": "^2.7", "laravel/framework": "^6.0", "laravel/tinker": "~2.0", "laravelcollective/html": "^6.0", "maatwebsite/excel": "~3.1", "mariuzzo/laravel-js-localization": "^2.0", "piwik/device-detector": "^3.12", "spatie/pdf-to-image": "^1.10", "spatie/laravel-image-optimizer": "^1.6" }, "require-dev": { "fakerphp/faker": "^1.9.1", // Replace fzaninotto/faker "mockery/mockery": "^1.3", "phpunit/phpunit": "^7.5" }, "scripts": { "post-root-package-install": [ "php -r \"file_exists('.env') || copy('.env.example', '.env');\"" ], "post-create-project-cmd": [ "php artisan key:generate" ], "post-install-cmd": [ "Illuminate\\Foundation\\ComposerScripts::postInstall" // Remove php artisan optimize ], "post-update-cmd": [ "Illuminate\\Foundation\\ComposerScripts::postUpdate" // Remove php artisan optimize ] } }
Then run:
composer update
Resolve any dependency conflicts (use composer why [package-name] to see why a package is required). After updating:
- Run
php artisan migrateto check database compatibility. - Fix any code breaking changes (e.g., Laravel 6 removed some old helper functions—install
laravel/helpersif you still need them, or refactor to use Facades).
Step 3: Upgrade to Laravel 7.x
Repeat the process with these adjustments:
- Set
laravel/frameworkto^7.0 - Update PHP requirement to
^7.3 - Update dependencies like
laravelcollective/htmlto^7.0,phpunit/phpunitto^8.5 - Run
composer updateand fix any new conflicts. - Check Laravel 7's breaking changes (e.g., route caching updates, mail API changes) and adjust your code accordingly.
Step 4: Upgrade to Laravel 8.x
Final jump:
- Set
laravel/frameworkto^8.0 - PHP can stay at 7.3+ or upgrade to 8.0+
- Update dependencies like
laravelcollective/htmlto^8.0,phpunit/phpunitto^9.5 - Run
composer update - Address Laravel 8's breaking changes:
- Model factories were rewritten (from closures to classes)—you'll need to refactor your factories or use the legacy factory package.
- Route namespaces are disabled by default—update your route files to use full controller class names, or re-enable namespaces in
RouteServiceProvider.
Pro Tips for Dependency Conflicts
- If a package doesn't support your target Laravel version, check its GitHub repo for forks or alternative packages.
- For packages tagged
dev-master, try switching to a stable release if available (dev versions are risky and often cause conflicts). - Use
composer update --with-all-dependenciesto force update all dependent packages if you're stuck.
内容的提问来源于stack exchange,提问作者Akshay K Nair

