I am trying to search my products table with the following form.
I want people to have the option to be able to just search by 1 filter or all filter options.
The issue I am running into is that then sometimes field values can/will be null OR equal to 'any'.
So how can I run a query where if 'any' is selected, it will return all results for that filter, and if null is the value it will do the same?
My current code below.
Form
<form method="GET" action="/">
<input type="text" name="search_location">
<input type="text" name="search_keywords">
<select class="wide" name="color">
<option value="any">Any Color</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
<select class="wide" name="price">
<option value="any">Any Price</option>
<option value="10"></option>
<option value="20"></option>
<option value="30"></option>
</select>
</form>
Controller
public function search(Request $request)
{
$location = $request->search_location;
$keywords = $request->search_keywords;
$color = $request->color;
$price = $request->price;
$products = Product::where('location', $location)
->where('keywords', $keywords)
->where('color', $color)
->where('price', $price)
->get();
dd($products);
}