Stumped on how to do a simple query off GET variables
Hi everyone,
I have a very simple property search form and I want to simply query my listing model based on the get variables.
The problem I'm having is when one of the GET variables isn't used (such as me putting in a min price and not a max price).
What is the proper way to search just by the variables in GET?
public function index(Request $request)
{
if($request->has('minprice')) {
$minprice = $request->minprice;
}
if($request->has('maxprice')) {
$maxprice = $request->maxprice;
}
if($request->has('beds')) {
$beds = $request->beds;
}
if($request->has('baths')) {
$baths = $request->baths;
}
$listings = Listing::where('CurrentPrice', '>=', $minprice)
->where('CurrentPrice', '<=', $maxprice)
->take(20)->get();
foreach($listings as $listing) {
echo $listing->MLSNumber . "<br>";
}
}
Then you need to check if the max price is set and either have to queries one with max price and one without.
Or you can simply add and absurd amount to it.
if($request->has('maxprice')) {
$maxprice = $request->maxprice;
} else {
$maxprice = 999999999999999;
}
Several solutions and the simplest one :
...
$maxprice=9999999
if($request->has('maxprice')) {
...
same as @tray2 who was faster :-)
Please or to participate in this conversation.