phplaravel

How do I change the hard code user id into the authenticated user?


In my controller I have the following code

    $assignents =Assign::whereHas('users', function ($q) {
        $q->where('user_id', 3); 
    })->get();

I would like to have the number 3 be the authenticated user who is logged in. How can I rewrite this code?


Solution

  • You can use the Auth facade to retrieve the authenticated user.

    $assignents = Assign::whereHas('users', function ($q) {
        $q->where('user_id', Auth::user()->id); 
    })->get();