Laravel 6.x→7.xバージョンアップ

# Laravel 6.x→7.xバージョンアップ方法

## 概要
Laravelのバージョンを6.x→7.xにアップする方法をご紹介します。
基本的には公式ドキュメント様の言う通りです。
なお、今回ご紹介するのはバージョンアップ対応で必要な最低限の項目になります。
詳細は [公式ドキュメント](https://laravel.com/docs/7.x/upgrade) を参照ください。

## 手順

### 1. 現在のバージョン確認
```bash
% php artisan --version
Laravel Framework 6.18.25
```

### 2. composer.jsonの編集
composer.jsonを直接触るのは嫌なのですが、、、公式がそう記載しているので仕方ないですね。

#### 前提
- Symfony 5以上
- PHP 7.2.5以上

#### 編集要点
- laravel/framework: "^7.0"
- nunomaduro/collision: "^4.1"
- phpunit/phpunit: "^8.5"
- laravel/tinker: "^2.0"
- facade/ignition: "^2.0"

#### Before
```json:composer.json
{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.2",
        "ext-fileinfo": "*",
        "barryvdh/laravel-dompdf": "^0.8.5",
        "bensampo/laravel-enum": "^1.28",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "^6.2",
        "laravel/tinker": "^1.0",
        "laravel/ui": "^1.2",
        "nesbot/carbon": "^2.27"
    },
    "require-dev": {
        "barryvdh/laravel-ide-helper": "^2.6",
        "facade/ignition": "^1.4",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^3.0",
        "phpunit/phpunit": "^8.0"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}
```

#### After
```json:composer.json
{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.2",
        "ext-fileinfo": "*",
        "barryvdh/laravel-dompdf": "^0.8.5",
        "bensampo/laravel-enum": "^1.28",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "^7.0",
        "laravel/tinker": "^2.0",
        "laravel/ui": "^1.2",
        "nesbot/carbon": "^2.27"
    },
    "require-dev": {
        "barryvdh/laravel-ide-helper": "^2.6",
        "facade/ignition": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^4.1",
        "phpunit/phpunit": "^8.5"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}
```

### 3. App\Exceptions\Handlerの編集
`app/Exceptions/Handler.php`を編集します。

#### 編集要点
`Exception`から`Throwable`に変更します。
`注: ちゃんとparentを継承するようにしないと後々エラーになってしまいます。`
詳しくは`After`を参照してください。

```php
use Throwable;

public function report(Throwable $exception);
public function shouldReport(Throwable $exception);
public function render($request, Throwable $exception);
public function renderForConsole($output, Throwable $exception);
```

#### Before
```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
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

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

#### After
```php
<?php

namespace App\Exceptions;

use Throwable;
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  Throwable  $exception
     * @return void
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    public function shouldReport(Throwable $exception)
    {
        parent::shouldntReport($exception);
    }

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

    public function renderForConsole($output, Throwable $exception)
    {
        parent::renderForConsole($output, $exception);
    }
}
```

### 4. sessionファイル編集
`app/config/session.php`を編集します。

#### Before
```php
'secure' => env('SESSION_SECURE_COOKIE', false),
```

#### After
```php
'secure' => env('SESSION_SECURE_COOKIE', null),
```

### 5. Composer update
```bash
% composer update
```

### 6. Laravel/uiの設定
composerの`require`セクションにて"2.0"以上が必要なようです。

```bash
% composer require laravel/ui "^2.0"
```

### 7. 確認
```bash
% php artisan --version
Laravel Framework 7.19.1
```


## 参考
- [Upgrade Guide](https://laravel.com/docs/7.x/upgrade#authentication-scaffolding)
- [PHP: Laravel 7へのアップグレードを試した](https://techracho.bpsinc.jp/wingdoor/2020_04_08/90650)