public function setRole($role) { $cacheKey = $this->getPermissionsCacheKey(); if (is_numeric($role)) { $role = Role::findOrFail($role); goto modelSave; } $role = Role::where('name', '=', $role)->firstOrFail(); modelSave: $this->permissionsRelation()->delete(); //Delete all permissions Cache::forget($cacheKey); $authUsername = \Auth::user() ? \Auth::user()->username : '******'; \Log::info('SET: Role of "' . $role->name . '" to user "' . $this->username . '" by user "' . $authUsername . '".'); if (!$this->roleRelation) { $userRole = new UserRole(['user_id' => $this->id, 'role_id' => $role->id]); $this->roleRelation()->save($userRole); return; } $this->roleRelation->role_id = $role->id; $this->roleRelation->save(); }