getLftName(), $this->getRgtName()]; } /** * A role belongs to some users of the model associated. */ public function accounts(): BelongsToMany { return $this->belongsToMany(Account::class, 'account_has_roles', 'role_id', 'account_id'); } public static function findById(int $id): Role { $role = static::where('id', $id)->first(); if (!$role) { throw new NotExistException('角色不存在'); } return $role; } /** * Determine if the user may perform the given permission. * * @param string|Permission $permission * * @return bool */ public function hasPermissionTo($permission): bool { if (is_string($permission)) { $permission = app(Permission::class)->findByName($permission); } if (is_int($permission)) { $permission = app(Permission::class)->findById($permission); } return $this->permissions->contains('id', $permission->id); } }