How to get current user id in laravel 5.4

I used this code in Laravel 5.4 to get the current logged in user id

    $id = User::find(Auth::id());
    dd($id);

      

but i get "Null"

+7


source to share


3 answers


You need to call the method user()

:

$id = \Auth::user()->id;

      



Or, if you only want to get the model:

$user = \Auth::user();

      

+15


source


You can access the authenticated user using the Auth facade:

use Illuminate\Support\Facades\Auth;

// Get the currently authenticated user...

$user = Auth::user();

// Get the currently authenticated user ID...

$id = Auth::id();

      

You can access the authenticated user with Illuminate \ Http \ Request



use Illuminate\Http\Request;
public function update(Request $request)
{
     $request->user(); //returns an instance of the authenticated user...
     $request->user()->id; // returns authenticated user id. 
}

      

via the Auth helper function:

auth()->user();  //returns an instance of the authenticated user...
auth()->user()->id ; // returns authenticated user id. 

      

+2


source


Using the helper:

auth()->user()->id ;   // or get name - email - ...

      

Using the facade:

\Auth::user()->id ;   // or get name - email - ...

      

0


source







All Articles