«

mineadmin mapper使用

时间:2024-7-25 20:46     作者:xiang     分类:


SystemMenuMapper

  /**
     * 获取子孙menus.
     */
    public function getDescendantsMenus(int $id): array
    {
        $params = ['level' => $id];
        return $this->handleSearch($this->model::query(), $params)->get()->toArray();
    }

    /**
     * 搜索处理器.
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', $params['status']);
        }

        if (isset($params['level']) && filled($params['level'])) {
            $query->where('level', 'like', '%' . $params['level'] . '%');
        }

        if (isset($params['name']) && filled($params['name'])) {
            $query->where('name', 'like', '%' . $params['name'] . '%');
        }

        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [$params['created_at'][0] . ' 00:00:00', $params['created_at'][1] . ' 23:59:59']
            );
        }

        if (isset($params['noButton']) && filled($params['noButton']) && $params['noButton'] === true) {
            $query->where('type', '<>', SystemMenu::BUTTON);
        }
        return $query;
    }