ErrorException Undefined property: Illuminate\Auth\SessionGuard::$id

I have create admin in laravel. so now i want to post as a admin to database but when i did i got error like above. i have a Post Table, I have admin id, I have admin table but it couldn't work. // To create post

$post =  new Post;
    $post->title = $request->input('title');
    $post->body = $request->input('body');
    $post->admin_id = Auth()->guard('admin')->id;
    $post->save();

I did like this above but i got error. I also tried as

$post->admin_id = Auth()->admin()->id;

i got admin() not found. I did Everything as per instructions but i got error.

$post->user_id = Auth()->user()->id;

Whereas this works i don't know what to do. i did everything as per instructions in Github but I have a trouble here. Please help me Guard declaration:

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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'admin-api' => [
            'driver' => 'token',
            'provider' => 'admins',
        ],
    ],

Upvotes: 0

Views: 7503

Answers (3)

upcoming
upcoming

Reputation: 23

Auth::guard('admin')->user()->id;

Upvotes: 0

Mahedi Hasan
Mahedi Hasan

Reputation: 1078

try with it

auth()->guard('admin')->id();

Upvotes: 1

Sandesh Poudel
Sandesh Poudel

Reputation: 93

please check your code it should be like this.

auth()->id()

Upvotes: 6

Related Questions