Level 24
You want to wrap thewhereHas() clauses in a ->where(function ($query) { }) closure?
https://laravel.com/docs/queries#parameter-grouping
Replace doesntHave() with orDoesntHave().
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I need to build query in which I will get projects with tasks that:
(current user belongs to project && project has tasks that are assigned to current user) or (project has tasks that are not assigned to anyone).
I stuck with this query:
$projects = Project
::with('tasks.status', 'tasks.users')
// this should be first condition where(
->whereHas('users', function ($query) use ($currentUser) {
$query->where('users.id', '=', $currentUser->id);
})
->whereHas('tasks.users', function ($query) use ($currentUser) {
$query->where('id', '=', $currentUser->id);
})
// ) end first where
->doesntHave('tasks.users') //this should be wrap into orWhere
->get();
You want to wrap thewhereHas() clauses in a ->where(function ($query) { }) closure?
https://laravel.com/docs/queries#parameter-grouping
Replace doesntHave() with orDoesntHave().
Please or to participate in this conversation.