在laravel中无法获得搜索组合

问题描述 投票:1回答:1

我有这个问题:

$products = Product::whereIn('brand_id', $brand)->get();

这是选定品牌的结果。但是,如果我想添加其他字段,如最小和最大价格,则返回错误,这是我的代码:

$brand = Input::has('brands') ? Input::get('brands') : [];
$min_price = Input::has('min_price') ? Input::get('min_price') : null;
$max_price = Input::has('max_price') ? Input::get('max_price') : null;

$products = Product::orWhere('price','>=',$min_price)
->orWhere('price','<=',$max_price)
->orWhereHas('brands',function($query){
  $query->whereIn('brand_id', $brand);
})->get();

错误我得到:

非法的运营商和价值组合。

php laravel
1个回答
0
投票

您需要检查输入数据,并且不要在这样的查询中允许null

// $max_price is null
->orWhere('price', '>=', $max_price)

其中一个输入字段是null,这就是你收到错误的原因。

你也应该add use() to the closure

function($query) use($brand) {
    $query->whereIn('brand_id', $brand);
})
© www.soinside.com 2019 - 2024. All rights reserved.