Php 如何根据Laravel中的GET变量进行路由

Php 如何根据Laravel中的GET变量进行路由,php,laravel,laravel-4,Php,Laravel,Laravel 4,我试图通过从URL获取变量名来进行路由。此变量将使用select附加到URL 所以我的URL将是:example.com/?city=Montreal 在route.php中,我有: Route::get('/?city={name}', 'HomeController@filterHome' ); Route::get('/', 'HomeController@home' ); 在HomeController.php中,我有 public function home(){ $nom=

我试图通过从URL获取变量名来进行路由。此变量将使用
select
附加到URL

所以我的URL将是:
example.com/?city=Montreal

在route.php中,我有:

Route::get('/?city={name}', 'HomeController@filterHome' );
Route::get('/', 'HomeController@home' );
HomeController.php
中,我有

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);
}

但这似乎不起作用。在这种情况下,在Laravel中路由的最佳方式是什么?

您不能将查询字符串放入路由定义中

使用一条路线和一项功能即可轻松处理:

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());
}

谢谢,这很有效。你能给我解释一下
Nom::query()的用法吗
@user1012181
Model::query()
启动一个Laravel“查询生成器”。当您执行
Nom::all()
时,它在内部执行
Nom::query()->get()
。一旦有了查询生成器,您就可以将各种数据库功能链接在一起,如
where
groupBy
join
has
,等等。