LoginSignup
0
0

More than 1 year has passed since last update.

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

Posted at

①usersモデルの複製

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

パス:~/app/Models/

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'],
        ]);
    }

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0