dembilesmana
4 months ago
793
5
Laravel

how to login with different models in laravel?

Posted 4 months ago by dembilesmana

I have tried to solve this problem for a few days and haven't resolved it yet, hehehe. so please help the masters here please help.

First I change the name user.php to client.php.

Then I made a new model named admin.php.

then in the app/auth.php file I change it to be like this:

    'defaults' => [
        'guard' => 'client',
        'passwords' => 'clients',
    ],

    'guards' => [
        'client' => [
            'driver' => 'session',
            'provider' => 'clients',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
    ],

    'providers' => [
        'clients' => [
            'driver' => 'eloquent',
            'model' => App\Client::class,
        ],

        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
    ],

    'passwords' => [
        'clients' => [
            'provider' => 'clients',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],

        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

and this LoginController.php:

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function showLoginForm()
    {
        return view('admin.auth.login');
    }

    protected function guard()
    {
        return Auth::guard('admin');
    }

then I tried to log in, and the result was session set, but displaying the login page again (no login failed) it's just that I was not directed to the home page

then I change the auth.php file:

    'defaults' => [
        'guard' => 'admin',
        'passwords' => 'admins',
    ],

then I open http://app.dev/login and I am immediately directed to the homepage, which means I have successfully logged in with a session that was previously saved.

the question: how to login with different models in laravel? or where/how is Illuminate\Foundation\Auth\AuthenticatesUsers; call the defaults array value?

Please sign in or create an account to participate in this conversation.