【发布时间】:2016-06-12 18:28:35
【问题描述】:
所以如果用户没有选择任何选项,我想获取所有项目,否则根据请求数据查询项目。但是我不能将$request 传递给我的函数。这是我的代码:
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();
}
}
显然,我的 showProducts 闭包中有 $request,但是我无法在 where 内的匿名函数中访问它。如何在匿名函数中使用我的 $request 数据?
【问题讨论】:
标签: php laravel request eloquent