php - 具有多个输入的 Laravel 高级查询
问题描述
我有一些带有多个参数的搜索,我想查询输入是否不为空,然后进行查询参数。
我尝试了很多方法,但我无法获得查询结果。
这是我的示例代码
$products = Product::where('game_id',$tg->id);
if($keyword !=null){
$products->where("name","like",$keyword);
}
if($price_start !=null){
$products->where("price",">",$price_start);
}
if($price_end !=null){
$products->where("price","<",$price_end);
}
if($avalibility !=null){
$products->where("status","=",$avalibility);
}
$products->orderBy("created_at","DESC");
$products->get();
解决方案
在您的代码中,将查询结果分配给变量 products,如下所示:
$products = $products->get();
现在 $products 将包含结果
推荐阅读
- javascript - Gravity Forms 带有美国国家代码的电话面罩
- vim - vim x/y 滚动条隐藏我的命令,真的很讨厌
- r - Prestige$type 中的错误:“闭包”类型的对象不是子集
- npm - 节点应用程序 - 有大量的哈希查找文件
- python - 将不同类型的缺失值转换为 pandas 可识别的 NA
- python - 如何更改 Seaborn 条形图中的标签字体大小?
- ruby-on-rails - Ruby on Rails 插入错误 ***** PG::InsufficientPrivilege: 错误: 关系发票的权限被拒绝
- python - python使用“模板列表”创建新列表
- r - 如何从 0 开始计算观察的出现次数并将其存储在 R 中的新变量中?
- javascript - Javascript script-src 使页面加载两次?