又又发 Package 了~拯救 where 地狱的 Eloquent 过滤器~

你可能写过这样的函数:

    public function getAppsList(AppListSearchRequest $searchRequest)
    {
        $projects = Project::statusActive()->with('projectable', 'user')->where('projectable_type', 'App\App');
        if ($searchRequest->min_price) {
            $projects = $projects->where('price', '>=', $searchRequest->min_price);
        }
        if ($searchRequest->max_price) {
            $projects = $projects->where('price', '<=', $searchRequest->max_price);
        }

        if ($searchRequest->min_monthly_downloads || $searchRequest->max_monthly_downloads || $searchRequest->min_monthly_income || $searchRequest->max_monthly_income) {
            $projects = $projects->whereIn('projectable_id', function ($q) use ($searchRequest) {
                $q = $q->from('apps');
                if ($searchRequest->min_monthly_downloads) {
                    $q = $q->where('monthly_downloads', '>=', $searchRequest->min_monthly_downloads);
                }
                if ($searchRequest->max_monthly_downloads) {
                    $q = $q->where('monthly_downloads', '<=', $searchRequest->max_monthly_downloads);
                }
                if ($searchRequest->min_monthly_income) {
                    $q = $q->where('monthly_income', '>=', $searchRequest->min_monthly_income);
                }
                if ($searchRequest->max_monthly_income) {
                    $q = $q->where('monthly_income', '<=', $searchRequest->max_monthly_income);
                }
                $q->lists('id');
            });
        }

        // platforms
        $platforms = $searchRequest->getPlatformsArray();
        $projects = $this->withAnyTags($projects, $platforms);

        // Categories
        if (@$searchRequest->category[0] || @$searchRequest->category[1]) {
            $categories = $searchRequest->getCategoriesArray();
            $projects = $this->withAnyTags($projects, $categories);
        }

        $projects = $projects->orderByTop()->paginate($searchRequest->page_size);
        return $projects;
    }

一大堆where头晕? 那么你需要 zgldh/laravel-query-filter

以后只需要

  1. 定义好过滤器
  2. 准备好过滤参数
  3. 得到过滤后的结果

就这么简单! 详情点击 zgldh/laravel-query-filter Github

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!