W
W
WebDev2016-03-17 16:07:01
Laravel
WebDev, 2016-03-17 16:07:01

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');
}

How can I change the users() method to return users of a specific profession?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-03-17
@kirill-93

$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 question

Ask a Question

731 491 924 answers to any question