首页 > 解决方案 > 具有多个输入的 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();

标签: phpmysqllaraveleloquent

解决方案


在您的代码中,将查询结果分配给变量 products,如下所示:

$products = $products->get();

现在 $products 将包含结果


推荐阅读