I believe this should work; not tested:
Task::where('status', 0)->whereHas('reports', function ($builder) {
$builder->where('department_id', 2)
->wherePivot('type', 2);
})->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have 3 tables
reports(id,title,department_id)
tasks(id, title, status, created_at)
task_relationship(id,task_id,report_id,type)
I want to select result for below conditions
1- I want to select all tasks with status=0
2- department_id = 2
3- type = 2
Department_id is in reports and their relation is saved in task_relationship table.
Can it be achieved by eloquent?
I believe this should work; not tested:
Task::where('status', 0)->whereHas('reports', function ($builder) {
$builder->where('department_id', 2)
->wherePivot('type', 2);
})->get();
Please or to participate in this conversation.