I have 2 models - User and RoleUser. Each user is assigned to a role. So I defined a one-to-one relationship by declaring a 'role' method on the User model.
public function role(){
return $this->hasOne('Vanguard\RoleUser', 'user_id');
}
This is my Role Model
class RoleUser extends Model
{
//
public $table = 'role_user';
protected $fillable = ['user_id', 'role_id'];
}
In controller, I am trying to fetch users with role_id = 2 but the query keeps returning all users (i.e. 3) instead of just one user. This is my controller
$users = User::with(['role' => function ($query) {
$query->where('role_id', 2);
}])->get();
Please what is causing this?