【Laravel8 laravel/ui】既存のusersテーブルからお好みのテーブルに変更する方法を分かりやすく素早く解説してみた

#①usersモデルの複製
マイグレーションで作成しても大丈夫ですが、こっちの方が簡単だと思うので簡単な方を説明します。Custom.phpは適当に付けた名称なので、ファイル名は好きに決めてもらって大丈夫です。

パス：~/app/Models/

```bash
cp User.php Custom.php
```

#②Custom.phpの編集
クラス名を変更するだけです
###class User extends Authenticatable

###↓

###class Custom extends Authenticatable

#③~/config/auth.phpの編集

providers => users内の**User::class**を先程作成したファイル名と同じく**Custom::class**に変更して下さい。

```
  /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
```

###↓

```
  /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\Custom::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
```

##④~/app/Http/Controllers/Auth/RegisterController.phpの編集

**use App\Models\User;**
**↓ ①で複製したファイルの名前に変更してください**
**use App\Models\Custom;**

```
    protected function create(array $data)
    {
        //ここを変更する
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
```

###↓

```
    protected function create(array $data)
    {
        //ここを変更する
        return Custom::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
```

```
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], //←を変更する
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }
```

###↓

```
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:テーブル名'],　//←を変更する
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }
```

#⑤
終わりです
素早く且つ分かりやすく解説出来たでしょうか...