jeudi 29 décembre 2016

Retrieve models that belongsToMany specific models

I've got a standard many-to-many relationship

class User {
  public function roles() {
    return $this->belongsToMany('Role');
  }
}

class Role {
  public function users() {
    return $this->belongsToMany('User');
  }
}   

And it works very well.

But I need to select all the users that has exactly two specific roles.

$roleAdmin = Role::where('name', 'admin')->first();
$roleUser = Role::where('name', 'user')->first();

$users = //which users has BOTH $roleAdmin and $roleUser ??

Is it possible to achieve this using eloquent or I need a raw query?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hzQmpj
via IFTTT

Aucun commentaire:

Enregistrer un commentaire