M
M
mrdragon90002018-04-22 20:07:15
Laravel
mrdragon9000, 2018-04-22 20:07:15

How to get current user object in laravel along with eager loading of dependencies?

You need to get the current user object in the controller with eager dependency loading, specifically, the "groups" attached to it, and the "Groups" so that the "Messages" attached to them are also received.
Tried to do like this:

$user = $request->user()->with('groups.messages')->get();

As a result, $user contained a selection of all users from the database, and with greedily loaded groups and groups.messages. However, I only need the current user.
Try this:
use User;
...
$user = User::with('groups.messagesForSend')->find($request->user()->id);

It seems to do what it needs to. Gives the current user along with eager-loaded dependencies. But at least making an additional request to the database $request->user()->id is no longer correct
Tell me how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor, 2018-04-22
@mrdragon9000

Auth::user()->load('relationship');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question