Sasindu Jayampathi
Sasindu Jayampathi

Reputation: 362

Laravel advanced query with multiple inputs

I have some search with multiple arguments and I want to query if the input is not null then make query argument.

I tried many ways but I cannot get query results.

Here is my sample code

$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();

Upvotes: 0

Views: 417

Answers (4)

khaleelibrahim
khaleelibrahim

Reputation: 94

In your code assign the result of query to the variable products as follows:

$products = $products->get();

now the $products will contain the result

Upvotes: 2

Gorkhali Khadka
Gorkhali Khadka

Reputation: 835

Try this

 $products = Product::select('*')
                ->where(function ($query) use($request){
                            if($keyword !=null){
                                $query->where('name','like','%'.$keyword.'%');
                            }
                            if($price_start !=null){
                                $query->where("price",">",$price_start);
                            }
                            if($price_end !=null){
                                $query->where("price","<",$price_end);
                            }
                            if($avalibility !=null){
                                $query->where("status","=",$avalibility);
                            }
                        })
                ->where('game_id',$tg->id)
                ->orderBy('created_at', 'DESC')
                ->get();

Upvotes: 2

Md. Amirozzaman
Md. Amirozzaman

Reputation: 1125

First you need to validate your data as follow

$request->validate([
    'id' => 'required',
    'keyword' => 'required',
    'price_start' => 'required',
    'availability' => 'required'
]); 

Now you can perform your data query with these validate Data. If you have multiple data for where method you might be put all of them in array ,after that to fetch the data-you should use get() method; like as bellow

$products = Product::where([
              ['id', '=', $request->id],
              ['keyword', 'like', $request->keyword]
            ])
            ->orderBy("created_at","DESC")
            ->get();

Upvotes: 0

Tilieth
Tilieth

Reputation: 21

Try adding ->get() to the query builder.

$products = Product::where('game_id',$tg->id)->orderBy->('created_at', 'desc')->get().

Use the explicit ->get() on the query builder to get a collection. And then use laravels collection methods (https://laravel.com/docs/5.8/collections#available-methods) to add further where clauses and sort the results (see sortBy method) or add the orderBy clause to the intial query (as above). You can leave out the final ->get() since it's called on the query builder.

Using the 'where' collection method inside the if statement:

if(isset($keyword)) {
    $products = $products->where('name','like', '%' . $keyword . '%');
}

Upvotes: 1

Related Questions