Laravel: get current logged in user

To get the user id of a logged in user in Laravel you can use

1) the auth() helper

$id = auth()->user()->id;

2) you can use the Auth facade

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

The helper is a bit easier and more straightforward in my opinion. To use the facade you need to import the class

 

use Illuminate\Support\Facades\Auth;


Link::create([
            'url' => 'www.test.com',
            'description' => 'ccccc',
            'user_id' => $id = Auth::user()->id
        ]);