Use debugbar to see the actual query. Maybe that will help you spot the error. Paste it here if you need help
Dec 9, 2021
5
Level 7
How to make nested whereHas eloquent query
This is is the current query
$userId = 1;
$projectId = 1;
return Kanban::where('project_id', $projectId)
->whereHas('cards.users.user', function ($query) use ($userId) {
$query->where('user_id', $userId);
})
->get();
I am trying to find -> all kanbans where project id = 1 -> where also has cards associated with it, that also have a user with id of 1
I am not getting any errors, just a null result.
I tried to follow along this example
$packages = Package::where('alias','like',"%$search%")
->whereHas('product.membership.club.user', function ($q) use ($user) {
$q->whereId($user->id);
})->get();
Not sure what I messed up, any help is much apprciated
Level 7
For anyone that might stumble upon this in the future. He is an example of the nested queries
return Project::where('id', $projectId)
->with(['kanbans' => function($query ) use ($userId){
$query->whereHas('cards.users', function ($query) use ($userId) {
$query->where('user_id', $userId);
})
->with(
'cards.users',
'cards.tags.tag',
'cards.column'
)
->withCount('cards')
->with(['cards' => function($query){
$query->withCount(['links','comments','tasks']);
}]);
}])
->with('tags')
->firstOrFail();
});
Please or to participate in this conversation.

