【Laravel】5.8から8.xに段階的にアップグレードする
※本ページのリンクにはプロモーションが含まれています。
こんにちは、Ryohei(@ityryohei)です!
本記事では、Laravelのバージョンを5.8から8.xまで段階的にアップグレードする方法をご紹介しています。
Laravelのサポートが切れているしそろそろバージョンを上げたい…でも何をすればいいんだろう?
上記の疑問にお答えします。
では、解説していきます。アップグレードについて
Laravelでは各バージョンにおいてアップグレードガイドが用意されています。アップグレードの方法や変更点が詳しく記載されています。
本記事ではアップグレードガイド内のアップグレード説明を参考にアップグレードをしていきます。詳細についきましては下記ドキュメントをご確認いただければと思います。
5.x→6.0 | https://readouble.com/laravel/6.x/ja/upgrade.html |
6.x→7.0 | https://readouble.com/laravel/7.x/ja/upgrade.html |
7.x→8.0 | https://readouble.com/laravel/8.x/ja/upgrade.html |
5.8から6.xにアップグレードする
laravel
プロジェクト直下のcomposer.json
を修正します。以降もこちらのファイルをメインに修正していきます。Laravelの本体バージョンは、require
のlaravel/framework
の値に記載されています。5.8.*
となっている部分を^6.0
に変更することでアップグレードの対象とすることができます。
[修正前]composer.json
"require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "5.8.*", "laravel/tinker": "^1.0" },
[修正後]composer.json
"require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "^6.0", "laravel/tinker": "^1.0" },
composer.json
の修正が完了したら、アップデートします。laravel
プロジェクトの直下に移動し、下記コマンドを実行します。
$ composer update
実行するとログが流れます。私の環境で表示されたログを下記の通り。
Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 3 updates, 1 removal - Removing erusev/parsedown (1.7.4) - Upgrading doctrine/inflector (1.4.4 => 2.0.3) - Upgrading laravel/framework (v5.8.38 => v6.20.27) - Locking league/commonmark (1.6.2) Writing lock file Package operations: 1 install, 3 updates, 1 removal - Downloading league/commonmark (1.6.2) - Removing erusev/parsedown (1.7.4) - Upgrading monolog/monolog (1.26.1 => 2.2.0): Extracting archive - Installing league/commonmark (1.6.2): Extracting archive - Upgrading doctrine/inflector (1.4.4 => 2.0.3): Extracting archive - Upgrading laravel/framework (v5.8.38 => v6.20.27): Extracting archive Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested. Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested. Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: beyondcode/laravel-dump-server Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. 50 packages you are using are looking for funding. Use the `composer fund` command to find out more!
successfully
とアップデートが完了しているので、反映されているかをlaravel
のバージョンで確認します。
$ php artisan --version Laravel Framework 6.20.27
バージョンが6.20.27
になっています。サイトの表示も正常でしたので、5.8
から6.x
のアップグレードはこちらで完了です。
6.xから7.xにアップグレードする
6.x
から7.x
のアップグレードではファイルの修正が必要になるため、公式の手順を参考に進めていきます。
まずはcomposer.json
の修正から。本体と依存パッケージのバージョンを修正します。
[修正前]composer.json
"require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "^6.0", "laravel/tinker": "^1.0" }, "require-dev": { "beyondcode/laravel-dump-server": "^1.0", "filp/whoops": "^2.0", "fzaninotto/faker": "^1.4", "mockery/mockery": "^1.0", "nunomaduro/collision": "^3.0", "phpunit/phpunit": "^7.5" },
[修正後]composer.json
"require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "^7.0", "laravel/tinker": "^2.0", "laravel/ui": "2.0" }, "require-dev": { "beyondcode/laravel-dump-server": "^1.0", "filp/whoops": "^2.0", "fzaninotto/faker": "^1.4", "mockery/mockery": "^1.0", "nunomaduro/collision": "^4.1", "phpunit/phpunit": "^8.5", "facade/ignition": "^2.0" //←ない場合は追加 },
続いて変更が必要なファイルを修正していきます。
Symfony5のアップグレードに伴う修正
app/Exceptions/Handler.php
のException
をThrowable
に変更します。
//Use Exception; Use Throwable; //public function report(Exception $exception) public function report(Throwable $exception) //public function render($request, Exception $exception) public function render($request, Throwable $exception)
続いてconfig/session.php
を変更します。
//'secure' => env('SESSION_SECURE_COOKIE', false), 'secure' => env('SESSION_SECURE_COOKIE', null),
必要なファイルの修正完了後、アップデートを実行します。
$ composer update
ずらーっとログが流れます。
Loading composer repositories with package information Updating dependencies Nothing to modify in lock file Installing dependencies from lock file (including require-dev) Package operations: 0 installs, 0 updates, 1 removal - Removing laravel/ui (v2.0.0) Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested. Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested. Generating optimized autoload files > @php artisan package:discover --ansi RuntimeException In order to use the Auth::routes() method, please install the laravel/ui package. at C:\xampp\htdocs\project\laravel\vendor\laravel\framework\src\Illuminate\Support\Facades\Auth.php:56 52| */ 53| public static function routes(array $options = []) 54| { 55| if (! static::$app->providerIsLoaded(UiServiceProvider::class)) { > 56| throw new RuntimeException('In order to use the Auth::routes() method, please install the laravel/ui package.'); 57| } 58| 59| static::$app->make('router')->auth($options); 60| } 1 C:\xampp\htdocs\project\laravel\routes\web.php:14 Illuminate\Support\Facades\Auth::routes([]) 2 C:\xampp\htdocs\project\laravel\vendor\laravel\framework\src\Illuminate\Routing\RouteFileRegistrar.php:35 require("C:\xampp\htdocs\project\laravel\routes\web.php") Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
エラーメッセージが表示されます。laravel/ui
のパッケージが必要とのことなので、インストールしましょう。
$ composer require laravel/ui "^2.0"
しばらく待ちます。
./composer.json has been updated Loading composer repositories with package information Updating dependencies Lock file operations: 1 install, 0 updates, 0 removals - Locking laravel/ui (v2.0.0) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing laravel/ui (v2.0.0): Extracting archive Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested. Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested. Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: beyondcode/laravel-dump-server Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision 61 packages you are using are looking for funding. Use the `composer fund` command to find out more!
パッケージのインストール後、再度アップデートを実行します。
$ composer update
エラーが発生したときのために心の準備を…
Loading composer repositories with package information Updating dependencies Nothing to modify in lock file Installing dependencies from lock file (including require-dev) Nothing to install, update or remove Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested. Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested. Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: beyondcode/laravel-dump-server Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. 61 packages you are using are looking for funding. Use the `composer fund` command to find out more!
問題なくアップグレードが完了しました。念のためlaravel
のバージョンを確認します。
$ php artisan --version Laravel Framework 7.30.4
きちんとアップグレードされています。サイトの動作自体も問題なさそうです。
7.xから8.xにアップグレードする
ようやく最後のアップデートです。前項までと同様にcomposer.json
の内容を修正します。
[修正前]composer.json
"require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "^7.0", "laravel/tinker": "^2.0", "laravel/ui": "2.0" }, "require-dev": { "beyondcode/laravel-dump-server": "^1.0", "filp/whoops": "^2.0", "fzaninotto/faker": "^1.4", "mockery/mockery": "^1.0", "nunomaduro/collision": "^4.1", "phpunit/phpunit": "^8.5", "facade/ignition": "^2.0" },
[修正後]composer.json
"require": { "php": "^7.1.3", "fideloper/proxy": "^4.0", "laravel/framework": "^8.0", "laravel/tinker": "^2.0", "laravel/ui": "^3.0" }, "require-dev": { "beyondcode/laravel-dump-server": "^1.0", "filp/whoops": "^2.0", "fzaninotto/faker": "^1.4", "mockery/mockery": "^1.0", "nunomaduro/collision": "^5.0", "phpunit/phpunit": "^9.0", "facade/ignition": "^2.0" },
7.x→8.0
のアップデートでは他のファイルの修正は必要ないので、composer.json
の修正後にアップデートを実行します。
$ composer update
ログを眺めます。
Loading composer repositories with package information Updating dependencies Lock file operations: 6 installs, 22 updates, 1 removal - Removing phpunit/php-token-stream (4.0.4) - Upgrading dragonmantank/cron-expression (v2.3.1 => v3.1.0) - Locking graham-campbell/result-type (v1.0.1) - Upgrading laravel/framework (v7.30.4 => v8.46.0) - Upgrading laravel/ui (v2.0.0 => v3.3.0) - Upgrading nunomaduro/collision (v4.3.0 => v5.4.0) - Upgrading phpunit/php-code-coverage (7.0.14 => 9.2.6) - Upgrading phpunit/php-file-iterator (2.0.3 => 3.0.5) - Locking phpunit/php-invoker (3.1.1) - Upgrading phpunit/php-text-template (1.2.1 => 2.0.4) - Upgrading phpunit/php-timer (2.1.3 => 5.0.3) - Upgrading phpunit/phpunit (8.5.16 => 9.5.5) - Locking sebastian/cli-parser (1.0.1) - Locking sebastian/code-unit (1.0.8) - Upgrading sebastian/code-unit-reverse-lookup (1.0.2 => 2.0.3) - Upgrading sebastian/comparator (3.0.3 => 4.0.6) - Locking sebastian/complexity (2.0.2) - Upgrading sebastian/diff (3.0.3 => 4.0.4) - Upgrading sebastian/environment (4.2.4 => 5.1.3) - Upgrading sebastian/exporter (3.1.3 => 4.0.3) - Upgrading sebastian/global-state (3.0.1 => 5.0.3) - Locking sebastian/lines-of-code (1.0.3) - Upgrading sebastian/object-enumerator (3.0.4 => 4.0.4) - Upgrading sebastian/object-reflector (1.1.2 => 2.0.4) - Upgrading sebastian/recursion-context (3.0.1 => 4.0.4) - Upgrading sebastian/resource-operations (2.0.2 => 3.0.3) - Upgrading sebastian/type (1.1.4 => 2.3.2) - Upgrading sebastian/version (2.0.1 => 3.0.2) - Upgrading vlucas/phpdotenv (v4.2.0 => v5.3.0) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 6 installs, 22 updates, 1 removal - Downloading graham-campbell/result-type (v1.0.1) - Downloading laravel/framework (v8.46.0) - Downloading laravel/ui (v3.3.0) - Downloading nunomaduro/collision (v5.4.0) - Downloading sebastian/version (3.0.2) - Downloading sebastian/type (2.3.2) - Downloading sebastian/resource-operations (3.0.3) - Downloading sebastian/global-state (5.0.3) - Downloading sebastian/exporter (4.0.3) - Downloading sebastian/environment (5.1.3) - Downloading sebastian/cli-parser (1.0.1) - Downloading phpunit/php-invoker (3.1.1) - Downloading phpunit/php-file-iterator (3.0.5) - Downloading sebastian/code-unit-reverse-lookup (2.0.3) - Downloading phpunit/php-code-coverage (9.2.6) - Downloading phpunit/phpunit (9.5.5) - Removing phpunit/php-token-stream (4.0.4) - Installing graham-campbell/result-type (v1.0.1): Extracting archive - Upgrading vlucas/phpdotenv (v4.2.0 => v5.3.0): Extracting archive - Upgrading dragonmantank/cron-expression (v2.3.1 => v3.1.0): Extracting archive - Upgrading laravel/framework (v7.30.4 => v8.46.0): Extracting archive - Upgrading laravel/ui (v2.0.0 => v3.3.0): Extracting archive - Upgrading nunomaduro/collision (v4.3.0 => v5.4.0): Extracting archive - Upgrading sebastian/version (2.0.1 => 3.0.2): Extracting archive - Upgrading sebastian/type (1.1.4 => 2.3.2): Extracting archive - Upgrading sebastian/resource-operations (2.0.2 => 3.0.3): Extracting archive - Upgrading sebastian/recursion-context (3.0.1 => 4.0.4): Extracting archive - Upgrading sebastian/object-reflector (1.1.2 => 2.0.4): Extracting archive - Upgrading sebastian/object-enumerator (3.0.4 => 4.0.4): Extracting archive - Upgrading sebastian/global-state (3.0.1 => 5.0.3): Extracting archive - Upgrading sebastian/exporter (3.1.3 => 4.0.3): Extracting archive - Upgrading sebastian/environment (4.2.4 => 5.1.3): Extracting archive - Upgrading sebastian/diff (3.0.3 => 4.0.4): Extracting archive - Upgrading sebastian/comparator (3.0.3 => 4.0.6): Extracting archive - Installing sebastian/code-unit (1.0.8): Extracting archive - Installing sebastian/cli-parser (1.0.1): Extracting archive - Upgrading phpunit/php-timer (2.1.3 => 5.0.3): Extracting archive - Upgrading phpunit/php-text-template (1.2.1 => 2.0.4): Extracting archive - Installing phpunit/php-invoker (3.1.1): Extracting archive - Upgrading phpunit/php-file-iterator (2.0.3 => 3.0.5): Extracting archive - Installing sebastian/lines-of-code (1.0.3): Extracting archive - Installing sebastian/complexity (2.0.2): Extracting archive - Upgrading sebastian/code-unit-reverse-lookup (1.0.2 => 2.0.3): Extracting archive - Upgrading phpunit/php-code-coverage (7.0.14 => 9.2.6): Extracting archive - Upgrading phpunit/phpunit (8.5.16 => 9.5.5): Extracting archive 1 package suggestions were added by new dependencies, use `composer suggest` to see details. Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested. Generating optimized autoload files > Illuminate\Foundation\ComposerScripts::postAutoloadDump > @php artisan package:discover --ansi Discovered Package: beyondcode/laravel-dump-server Discovered Package: facade/ignition Discovered Package: fideloper/proxy Discovered Package: laravel/tinker Discovered Package: laravel/ui Discovered Package: nesbot/carbon Discovered Package: nunomaduro/collision Package manifest generated successfully. 68 packages you are using are looking for funding. Use the `composer fund` command to find out more!
スムーズにアップデート完了。念のためlaravelのバージョンを確認しておきます。
php artisan --version Laravel Framework 8.46.0
以上で5.8
から8.x
へのアップグレード完了です!
最後に
LaravelにはLTSがありますが、Laravelの8.xはLTSではないので、セキュリティは2022年9月6日までのサポートになります。来年には新しいバージョンにアップグレードする必要がありますが、1年の間はLaravel8でも戦えると考えると気持ち的に余裕が生まれますね!
今後もアップグレードのタイミングで実施した内容を本記事のような忘備録形式で残しておく予定ですので、今後アップグレードされる方の参考になれば幸いです。
以上、Laravelを5.8から8.xに段階的にアップグレードする方法のご紹介でした!