0

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();
2
  • You are not doing anything with the result of $products->get() Commented Jan 5, 2020 at 9:08
  • add ->get(); after all queries Commented Jan 5, 2020 at 9:13

4 Answers 4

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

This method worked for me. Thank you and others all. You Saved My Time
2

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

$products = $products->get();

now the $products will contain the result

Comments

1

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 . '%');
}

Comments

0

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

1 Comment

Arguments do not need to validate because query must run if required some inputs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.