skip to Main Content

I have my route set as

Route::any('/{brand?}/{type?}/{city?}', 'SearchController@index')->name('search');

I want to send from my controller query strings (Form GET params)

After searching I ended up with this but it does not work properly

return redirect()->route('search', [$brand->name, $type->name, 'search_model_from' => $request->search_model_from, 'search_model_to' => $request->search_model_to]);

which returns back

localhost:8000/toyota/avalon/2018?search_model_to=2019

I want to return

localhost:8000/toyota/avalon/?search_model_from=2018&search_model_to=2019

What I am trying to achieve in general is SEO friendly search functionality

2

Answers


  1. Maybe you should try to assign city as null like that :

    return redirect()->route('search', [
        'brand' => $brand->name, 'type' => $type->name, 
        'city' => '', 'search_model_from' => $request->search_model_from, 
        'search_model_to' => $request->search_model_to
    ]);
    
    Login or Signup to reply.
  2. I’m not sure but this could happen because you have defined 3 optional parameters in the route and as you are sending just two of them, this might takes the next (in this case ‘search_model_from’) as the third parameter for url.

    Maybe if you cast and set a default value to the optional parameters in your Controller, you won’t have that trouble, like this:

    public function index(string $brand='', string $type='', string $city='' , $other_parameters)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search