基于动态参数的函数调用内部控制器

时间:2017-10-07 07:01:25

标签: php laravel laravel-5

我有以下控制器我在哪里运行基于通过ajax的变量的一些查询。现在我的问题是如何使它更有效的手段而不检查每一个条件我可以使它动态它将检查变量并只调用该行语句?

EG。假设它只有$ category_id& $ min_price所以它只会运行那一行语句,所以我的查询变得更快,并且还在控制器中保存no_of_statements?

public function searchByCheckbox(Request $request)
    {  
        if( !empty($request->get('category_id'))|| 
            !empty($request->get('industry_id')) ||
            !empty($request->get('min_price'))      ||
            !empty($request->get('max_price'))  
          )
        {                   

        $query = DB::table('users')
           ->join('products','products.auth_id','users.id')
           ->Join('reviews','products.id','reviews.product_id')
           ->select('products.*','users.avatar',DB::raw('(sum(rating)/count(user_id)) as rating'))             
           ->where('products.status','=','1')           
           ->groupBy('products.id')   
           ->latest();

        if ($request->has('category_id')) {
            $query->WhereIn('products.category_id', $request->get('category_id'));
        }

        if ($request->has('industry_id')) {
            $query->WhereIn('products.industry_id', $request->get('industry_id'));
        }

        if ($request->get('min_price')!='0') {
            $query->Where('products.regular_price','>' ,$request->get('min_price'));
        }

        if ($request->get('max_price')!='0') {
            $query->Where('products.regular_price','<' ,$request->get('max_price'));
        }


        if($request->has('min_price') && $request->has('max_price')){
            $query->whereBetween('products.regular_price',
            [   $request->get('min_price'),
                $request->get('max_price')
            ]);
        }

        if ($request->has('industry_id')&& $request->has('category_id')) 
        {
            $query->orWhereIn('products.industry_id', $request->get('industry_id'));
            $query->orWhereIn('products.category_id', $request->get('category_id'));
        }

        if ($request->has('industry_id')&& $request->has('category_id') && $request->get('min_price')!='0' && $request->get('max_price')!='0') 
        {
            $query->orWhereIn('products.industry_id', $request->get('industry_id'));
            $query->orWhereIn('products.category_id', $request->get('category_id'));
            $query->orwhereBetween('products.regular_price',
            [   $request->get('min_price'),
                $request->get('max_price')
            ]); 
        }

        $products = $query->get();


        }
        else
        {


        $products = DB::table('users')
          ->join('products','products.auth_id','users.id')
          ->Join('reviews','products.id','reviews.product_id')
           ->select('products.*','users.avatar',DB::raw('(sum(rating)/count(user_id)) as rating'))                       
          ->where('products.status','=','1') 
          ->groupBy('products.id')          
         ->latest()
         ->get();  
        }

        return view('cart.ajax.product-result', ['products' => $products]);
    }

1 个答案:

答案 0 :(得分:0)

我认为动态检查会增加不必要的复杂性。我只想做一些改变

  1. 将过滤数据与您请求中的其他数据分开(如果有的话)。因此,过滤字段的名称应该与filter['min_price']
  2. 类似
  3. 删除最后两个if语句,因为它们没有任何意义。
  4. 我会这样做:

    public function searchByCheckbox(Request $request)
    {
        $filter = $request->has('filter')?$request->get('filter'):[];
        $products = [];
        $query = DB::table('users')
            ->join('products','products.auth_id','users.id')
            ->Join('reviews','products.id','reviews.product_id')
            ->select('products.*','users.avatar',DB::raw('(sum(rating)/count(user_id)) as rating'))
            ->where('products.status','=','1')
            ->groupBy('products.id')
            ->latest();
    
        if(empty($filter)){
            $products = $query->get();
        }else{
            if(empty($filter['min_price'])){
                $filter['min_price'] = 0;
            }
    
            if(!empty($filter['category_id'])){
                $query->WhereIn('products.category_id', $filter['category_id']);
            }
    
            if(!empty($filter['industry_id'])){
                $query->WhereIn('products.industry_id', $filter['industry_id']);
            }
    
            if(!empty($filter['max_price'])){
                $query->whereBetween('products.regular_price',
                    [   $filter['min_price'],
                        $filter['max_price']
                    ]);
            }else{
                $query->Where('products.regular_price','>', $filter['min_price']);
            }
        }
        return view('cart.ajax.product-result', ['products' => $products]);
    }