【发布时间】:2016-10-06 12:40:45
【问题描述】:
我想显示特定类别的条目。 通过调用这条路线:
Route::get('menues/{city?}', 'PagesController@menue');
我想显示特定城市的所有条目。
这是我的控制器:
public function menue($city = null) {
if ($city) {
$restaurants = User::with(['articles' => function ($q){
$q->nowpublished();
}])->where('city', '=', $city)->get();
} else {
$restaurants = User::with(['articles' => function ($q){
$q->nowpublished();
}])->where('city', '!=', $city)->get();
}
return view('pages.menues')
->withRestaurants($restaurants)
->withCity($city);
}
唯一不起作用的是,通过调用带有 {city} 的 url,我想显示数据库中不存在的所有条目。
使用上面的代码不会发生这种情况。我得到一个空白页。
我该如何解决这个问题?我的猜测是我的 else 语句中的代码显示了所有条目,但事实并非如此。
【问题讨论】:
-
你在传递一个id吗?
{city}是城市ID? -
您是否将这条路线放入并分组,如果是,请也分享该组路线?如果还有一个路由变量,那么在您的控制器函数中,第一个变量将是组路由变量。