I have tried this but it doesn't work as I need, I also get the results of all users and not only the current user mapped through the query.
$users = User::with(['tests' => function ($query) {
$query->with('results');
}])->get();
Hello,
I need to write a query and I search an easy way to do it with Eloquent, but it's quite difficult.
I need to retrieve all the tests and the results of these tests for all users. I will get a collection of users and for each one I will have the tests to which he has participated and for each tests his own results.
$users = User::with([
'tests',
'tests.results'
])->get();
The problem is that each test has much more results than only this user's results.
With this query, it retrieves all results from all users for the current test and not only the results binded to the user.
Can you help me ?
Thanks a lot ;).
V
@vincent15000 But the pivot table has user_id column.
$users = User::with(['tests.results' => function ($query) {
$query->whereColumn('test_user.user_id', 'results.user_id');
}])->get();
Please or to participate in this conversation.