Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rainbowhat's avatar

Laravel 5 Query Builder Search Filter

I'm looking for a way to search and filter a database table / query builder through the query string in Laravel 5.

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?

Basically, sometimes when filter is applied, there will be hasCoffeeMachine if not there wont be.

0 likes
9 replies
kgadzhev's avatar

You can use get request to accomplish this. Lets say you have a form in your view that looks like that:

<form method="GET">
    <input type="text" name="name">
    <input type="checkbox" name="hasCoffeeMachine" value="1"><span> Apply Filter</span>
</form>

You should specify the route in your routes.php file like that:

Route::get('/', 'SearchController@search');

In your SearchController you'd have a method search that takes the values and performs the query:

class SearchController extends Controller {

    public function search() {

        // Sets the parameters from the get request to the variables.
        $name = Request::get('name');
        $hasCoffeeMachine = Request::get('hasCoffeeMachine');

        // Perform the query using Query Builder
        $result = DB::table('customers')
            ->select(DB::raw("*"))
            ->where('name', '=', $name)
            ->where('has_coffee_machine', '=', $hasCoffeeMachine)
            ->get();

        return $result;
    }
}
3 likes
NicolasVH's avatar

How do I do this with an Input field on the same page? This is my question from: http://stackoverflow.com/questions/34799481/laravel-5-php-filter-results-with-input-value

I have this where I get all my tournaments $tournaments = Tournament::all();.

Now I want to let the user filter these on the go in the view... I would like to have an <input> where the user enters a few characters and then the results filter on tournaments that have those characters in the name.

I have found this* online but I dont know how to populate that $keyword. It would be best if the results filter after every character that is entered in the inputfield. If this is not possible, then make this a form that sends this $keyword to Controller and retrieves the new results on the same page! * $tournament = Tournament::where('name', 'LIKE', '%'.$keyword.'%')->get();

How do I do this? I don't know... Please provide some code in the answer.

Thx

gruntj's avatar

#jlrdw.. how can I get it??

1 like
jlrdw's avatar

It's just a little search form at top, just like on this forum. The search logic is in controller:

$query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);
1 like

Please or to participate in this conversation.