Laravel

Laravel6からLaravel8へアップグレードする方法

いつもご利用ありがとうございます。このブログは、広告費によって運営されています。

オススメ本
Web技術を勉強するなら、かなりオススメの雑誌です。毎月新しい発見があります。ついに最終号・・・、みなさん買いましょう!!
読んで損することはない名著。命名で悩むことが多い人はこの本がオススメです。

⇨ Laravel 記事の目次はこちら

Laravel6 から Laravel8 へアップグレードにチャレンジしてみました。composer.json に Laravel8 と編集して、composer update してみたら行けそうな気がしますが、どんなバージョン依存関係のエラーがでるのかみていきたいと思います。

変更前の Laravel6 のパッケージ状況はこんな感じです。

    "require": {
        "php": "^7.2.5|^8.0",
        "diglactic/laravel-breadcrumbs": "^6.1",
        "fideloper/proxy": "^4.4",
        "laravel/framework": "^6.20",
        "laravel/tinker": "^2.5",
        "league/flysystem-aws-s3-v3": "~1.0",
        "weidner/goutte": "^1.6"
    },
      "require-dev": {
        "facade/ignition": "^1.16.4",
        "fakerphp/faker": "^1.9.1",
        "laravel/ui": "^1.0",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^3.0",
        "phpunit/phpunit": "^8.5.8|^9.3.3"
    },

結論(修正後)

composer.json

    "require": {
        "php": "^7.2.5|^8.0",
        "diglactic/laravel-breadcrumbs": "^6.1",
        "fideloper/proxy": "^4.4",
        "laravel/framework": "^8.0",
        "laravel/tinker": "^2.5",
        "league/flysystem-aws-s3-v3": "~1.0",
        "weidner/goutte": "^1.6"
    },
    "require-dev": {
        "facade/ignition": "^2.3.6",
        "fakerphp/faker": "^1.9.1",
        "laravel/ui": "^3.0",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^8.5.8|^9.3.3"
    },

このあとに少し他ファイルも触らないとエラーになります。

app/Exceptions/Handler.php を修正する

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Exception
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
}

アップグレード方法

composer.json の Laravel のバージョン指定を変更する

"laravel/framework": "^8.0",

コマンド

composer update

すると、バージョンエラーが発生する

  Problem 1
    - Conclusion: don't install laravel/framework v8.0.0 (conflict analysis result)
    ~~~~~~~~~~~~~~~
       - laravel/ui[v1.0.0, ..., 1.x-dev] require illuminate/support ~5.8|^6.0 -> satisfiable by illuminate/support[v5.8.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev].
    - Only one of these can be installed: illuminate/support[v5.0.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev], laravel/framework[v8.0.0, ..., 8.x-dev]. laravel/framework replaces illuminate/support and thus cannot coexist with it.
    - Root composer.json requires laravel/framework ^8.0 -> satisfiable by laravel/framework[v8.0.0, ..., 8.x-dev].
    - Root composer.json requires laravel/ui ^1.0 -> satisfiable by laravel/ui[v1.0.0, ..., 1.x-dev].

laravel/ui のバージョンが合っていないので3系を入れる

  "laravel/ui": "^3.0",

コマンド

composer update

symfony でバージョンがエラーになる

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires facade/ignition ^1.16.4 -> satisfiable by facade/ignition[1.16.4, ..., v1.x-dev].
    - You can only install one version of a package, so only one of these can be installed: symfony/console[v2.3.10, ..., 2.8.x-dev, v3.0.0-BETA1, ..., 3.4.x-dev, v4.0.0-BETA1, ..., 4.4.x-dev, v5.0.0-BETA1, ..., 5.4.x-dev, 6.0.x-dev].
    - Conclusion: install symfony/console v5.1.0 (conflict analysis result)

以下のように変更する

    "require-dev": {
        "facade/ignition": "^2.3.6",
        "nunomaduro/collision": "^5.0",

これで、アップグレード自体はうまくいきました。

しかしエラーが出たので次の項目で。

ファイルの修正

エラーの内容

> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 255

アップグレード後、上記のようなエラーで php artisan 系コマンドでエラーになる。

app/Exceptions/Handler.php の修正

このファイルが Laravel 6と8で内容が違っているのでエラーが出るようです。

内容をまるっと Laravel8 仕様に変更

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Exception
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }
}

にまるっと修正(Larave8 のものに修正)して、

composer update
php artisan -v
Laravel Framework 8.60.0

無事 Laravel8 になりました!

関連記事

⇨Laravel8 の認証を Jetstream で実装する方法

まとめ

以上です。

おそらく似たような記事はたくさんあると思いますが、参考になればと思います。

感想や意見ありましたら Twitter の DM などからご連絡いただければと思います!

それでは!

人気記事

PHP7.4 + Laravel6 のプロジェクトを AWS EC2 にデプロイする

【laravel-breadcrumbs】Laravel でパンくずリストを実装する