I'm looking for a way to search and filter a database table / Eloquent model through the query string in Laravel 4.
I have a table named houses
with the columns named: price
, name
, hasCoffeeMachine
and hasStove
I want the user to be able to do something like: http://example.com?name=test&hasCoffeeMachine=1
Which will get all the rows where the name is "test" and they have a coffee machine.
I want the user to be able to add URL parameters based on the filter they desire. What is the best way to account for all the possible combinations of query strings to return the correct results from the table?
$houses = new House;
if(Input::get('name'))
$houses->where('name', '=', Input::get('name'));
if(Input::get('hasCoffeeMachine'))
$houses->where('hasCoffeeMachine', '=', Input::get('hasCoffeeMachine'));
// Keep adding more for every filter you have
// Don't do this till you are done adding filters.
$houses = $houses->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With