Hi all.
I have 3 tables in Laravel: doctors, clinics and clinic_doctors. Doctors have relation:
public function clinics()
{
return $this->belongsToMany('\App\Models\Clinic', 'clinic_doctor');
}
And I try to get all doctors with TOP clinics:
$doctors = Doctor::with(['clinics' => function($query){
$query->where('clinics.top', true);
}])
->where('doctors.active', 1)
->select('doctors.id')
->orderByRating()
->get();
(instead TRUE I've tried 'true', 1, '1' - with the same result)
This query returns 1662 records - it's wrong result! But when I get SQL query from:
dd(\DB::getQueryLog());
and execute it directly in MySQL it returns 119 records. And this is right result.
What am I doing wrong with Eloquent?
Found solution: have changed "with" to "whereHas", and now it works!
Doctor::whereHas('clinics', function($query){
$query->where('clinics.top', true);
}])
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community