php - Getting Authenticated User and Pivot Table in Laravel -


the code below returning users in database. looking retrieve logged in user services (many-to-many relationship).

    $user_id = auth::user()->id;     dd(user::with('services')->find($user_id)->get()->toarray()); 

any appreciated.

the find() method retrieves user object. you're calling get() on user instance, retrieving collection of users. code work, remove call get().

one note, though: auth::user() instance of logged in user. there no need retrieve user again. access services user, can either explicitly load them using load() method, or can access services attribute lazy loaded first time used.

$user = auth::user(); // explicitly loading services: $user->load('services'); dd($user->toarray()); 

or

$user = auth::user(); // services lazy loaded on first access of attribute $services = $user->services; dd($user->toarray()); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -