2016-06-12 65 views
0

因此,如果用戶沒有選擇任何選項,我想獲取所有項目,否則根據請求數據查詢項目。但是我無法將$request傳遞給我的函數。這裏是我的代碼:Laravel - 如何在匿名函數中傳遞請求數據

public function showProducts(Request $request) 
{ 
     $products = Product::all(); 
     if(count($request->all()) != 0) { 
       $products = Product::where(function($query) { 
        $minPrice = $request['min'] ? $request['min'] : null; 
        $maxPrice = $request['max'] ? $request['max'] : null; 
        $colors = $request['color'] ? $request['color'] : null; 
        $sizes = $request['size'] ? $request['size'] : null; 

        if($minPrice != null && $maxPrice != null) { 
         $query->where('price', '>=', $minPrice)->where('price', '<=', $maxPrice); 
        } 

        if($minPrice == null && $maxPrice == null && $colors == null && $sizes == null) { 

        } 
       })->get(); 
      } 
} 

很顯然,我有我的展示Products關閉$請求,但是我不能訪問我的匿名函數內內where。我如何在匿名函數中使用我的$ request數據?

回答

相關問題