How to get logged in user id in Laravel 9

How to get logged in user id in Laravel 9

Posted by Luke Beeno on July 16, 2022

How to get logged in user id in Laravel 9

How to get logged in user id in Laravel 9

Posted by Luke Beeno on July 16, 2022

In order to get the logged in user id, you can use the Auth Fascade

If you want to call it from the controller, import it:

use Illuminate\Support\Facades\Auth;

In order to use it in the view, wrap routes in Auth middleware:

Route::group(['middleware' => ['auth']], function () { 
    Route::get('home', 'HomeController@index');
});

Now you can call the user id.

This is one way:

Auth::user()->id

This is another way:

Auth::id()

or like this:

auth()->id()

You can echo the user id:

<div>{{auth()->id()}}</div>

 

This field is required
Your question have been successfully submitted and will be reviewed before published
Please login to ask a question