Answer the question
In order to leave comments, you need to log in
Laravel get related model by condition?
Please tell me how to set a condition in such a structure. There are three models: Specialty User Country
Specialty, User, Country. The specialty is tied to the user, the user to the country.
How can I now select all users of the country who have a certain specialty?
Here is how the connection is described:
//Country
public function users()
{
return $this->hasMany('App\Models\User');
}
//User
public function specialties()
{
return $this->hasMany('App\Models\Specialty');
}
Answer the question
In order to leave comments, you need to log in
$country = Country::find(1);
$users = $country->users()->whereHas('specialties', function($query){
$query->where('speciality', 'doctor');
})->get();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question