예제 #1
0
 /**
  * Assign the given role to the user.
  *
  * @param string $role
  *
  * @return mixed
  */
 public function assignRole($role, $save = true)
 {
     // if we passed a role name, find it
     if (is_string($role)) {
         $role = RoleModel::whereName($role)->first();
     }
     // if user already has the role, return true.
     if ($save and $this->roles()->find([$role->id])->count()) {
         return $role;
     }
     // assign role
     if ($save) {
         return $this->roles()->save($role);
     } else {
         return $this->roles()->detach($role);
     }
 }