How to get loggedin customer information in laravel Multiple Authentication with database session driver?

918 Views Asked by At

I've two types of user admin (default users table) and customer (I created customers table). I've used a session with a database driver shipped with laravel-jetstream. I've no issue with the admin authentication. I'm getting user information successfully for admin. But when I log in with the customer, I do get logged in but not getting any logged-in customer information. I've checked the database.

auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'customer' => [
        'driver' => 'session',
        'provider' => 'customers',
    ],
],

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

customer login

public function login(Request $request) : object
{
    if (Auth::guard('customer')->attempt($request->only('email', 'password'))) {
        return redirect()->intended('/');
    }else {
        return back()->withErrors(['message' => 'Incorrect email or password']);
    }
}

My session table looks like below

Schema::create('sessions', function (Blueprint $table) {
        $table->string('id')->primary();
        $table->foreignId('user_id')->nullable()->index();
        $table->string('ip_address', 45)->nullable();
        $table->text('user_agent')->nullable();
        $table->text('payload');
        $table->integer('last_activity')->index();
    });

I understand $table->foreignId('user_id')->nullable()->index(); pointed to default user id. In case of customer, How do I store customer Id and retrieve information properly?

1

There are 1 best solutions below

0
On

I found my answer. I did a silly mistake.

{{ Auth::guard('customer')->user()->name }}

I missed the guard.