user1012181
user1012181

Reputation: 8726

How to route depending on GET variables in Laravel

I'm trying to route by getting the variable name from the URL. This variable will be append to the URL using a select.

So my URL will be like: example.com/?city=Montreal

In the route.php, I have:

Route::get('/?city={name}', 'HomeController@filterHome' );
Route::get('/', 'HomeController@home' );

and in the HomeController.php I have

public function home(){
    $nom= Nom::get();
    return View::make('home.index')->with('nom', $nom);
}

public function filterHome($place){
    $nom = Nom::where('place', '%LIKE%', $place)->get();
    return View::make('home.index')->with('nom', $nom);
}

But this don't seem to work. What is the best way to route in Laravel in this case?

Upvotes: 0

Views: 98

Answers (1)

ceejayoz
ceejayoz

Reputation: 180024

You can't put a query string in a route definition.

This can be handled easily with one route and one function:

Route::get('/', 'HomeController@home' );

public function home(){
  $nom= Nom::query();

  if(Input::get('city')) {
    $nom->where('place', 'LIKE', '%' . Input::get('city') . '%');
  }

  return View::make('home.index')->with('nom', $nom->get());
}

Upvotes: 2

Related Questions