LoginSignup
2
2

More than 5 years have passed since last update.

Laravel Auth のUserモデルを別モデルに書き換えたい

Posted at

ユーザー情報を保持し、それを閲覧する画面を作りたい時に困ったこと(Laravel5.5)
メモです。

<困ったこと>
:point_up:UserモデルをAdminに書き換えたい(Userはログインしない)
:v:Userモデルの時に作った認証機能を引き継ぎたい

<対処法>
1.
/config/auth.php

User ProvidersのモデルをApp\User::classからApp\Admin::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\Admin::class,
        ],
    ],

<困ったこと>
1.でログイン・ログアウトはできたけど、新規登録が出来ない:dizzy_face:

<対処法>
2.
/app/Http/Controllers/Auth/RegisterController.php

validatorファンクションの
emailのunique:usersをunique:adminsに書き換える

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:admins',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

ここはLaravel側でSQL叩いてるので、usersテーブルがないってエラーが出る

2
2
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
2
2