catchSearch() ->order('sort', 'desc') ->order('id', 'desc') ->when($isMenu, function ($query){ $query->where('type', self::MENU_TYPE); }) ->select(); } public function roles(): \think\model\relation\BelongsToMany { return $this->belongsToMany(Roles::class, 'role_has_permissions', 'role_id', 'permission_id'); } /** * 获取当前用户权限 * * @time 2020年01月14日 * @param array $permissionIds * @return \think\Collection * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\DataNotFoundException */ public static function getCurrentUserPermissions(array $permissionIds): \think\Collection { return parent::whereIn('id', $permissionIds) ->field(['permission_name as title', 'id', 'parent_id', 'route', 'icon', 'component', 'redirect', 'keepalive as keepAlive', 'hide_children_in_menu', 'type', 'permission_mark' ]) ->select(); } /** * 插入后回调 更新 level * * @time 2020年04月22日 * @param Model $model * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @return array|bool|Model|void|null */ public static function onAfterInsert(Model $model) { $model = self::where('id', $model->id)->find(); if ($model && $model->parent_id) { $parent = self::where('id', $model->parent_id)->find(); $level = $parent->level ? $parent->level . '-' . $parent->id : $parent->id; return $model->where('id', $model->id)->update([ 'level' => $level ]); } return true; } }