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

jpeterson579's avatar

How to make a url like domain.com/?&search=something trigger an eloquent query?

I would like to be able to create a url that has something looks like the following

domain.com/page?&search=something

which will then show the query results when the page is visited.

Is this possible? How? Good practice or bad?

0 likes
2 replies
topvillas's avatar

Yeah, sending querystrings is standard practice.

You don't need ?& just ?.

The search term will be available with $request->search.

rubol's avatar
rubol
Best Answer
Level 2

It's simple and easy. First you need to make function(method) that handles the search query and define route. Below is the code snippet from my project for search:

    public function search(Request $request)
        {
            $days = $request->days;
            $days_explode = explode('|', $days);

            $query = Tour::query();

            if (!empty($request->country)) {
                $query = $query->whereHas('country', function($r)       use($request) {
                $r->where('countries.name', $request->country);
                });
        }

        if (!empty($request->category)) {
            $query = $query->whereHas('category', function($s) use($request) {
                $s->where('categories.name', $request->category);
                });
        }

        if (!empty($request->days)) {
                $query = $query->whereBetween('days', [$days_explode[0],        $days_explode[1]]);
        }            

        $query = $query->paginate(8);        
            return view('public.tour.search')->withResults($query);
        }

And route is:

         Route::get('/trip/search','GetPublicController@search')
->name('search');

In my case I had used search form with multiple parameter(3 parameters). Less the no. of parameters, easy it will be.

And URL will be like this:

http://localhost:8000/trip/search?country=Croatia&category=cycling&days=1%7C15

Please or to participate in this conversation.