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數據?
工作就像一個魅力。不知道我必須使用它來做到這一點。謝謝! – Codearts