How to get all data from parent table with checking child table relationship where condition in laravel

Viewed 41

I have two tables Leads and Tasks, I need to fetch all leads by checking one specific column in the tasks table.

Laravel Mysql

Relation on Lead Model

 public function tasks()
{
    return $this->hasMany(Task::class, 'lead_id', 'id');
}

leads table

tasks table

*need to list all leads which create_task_id != 2 (tasks table)

1 Answers

if you want to check relation condition you should use whereHas() method.

Load::with('task')->whereHas('task', function ($query){
        $query->where('some condition')->get();
})->get();
Related