【发布时间】:2014-02-18 10:25:07
【问题描述】:
我需要检查{subcategory} 是否有父类别{category}。如何在第二次绑定中获得{category} 的模型?
我试过$route->getParameter('category');。 Laravel 抛出 FatalErrorException 并显示消息“已达到 '100' 的最大函数嵌套级别,正在中止!”。
Route::bind('category', function ($value) {
$category = Category::where('title', $value)->first();
if (!$category || $category->parent_id) {
App::abort(404);
}
return $category;
});
Route::bind('subcategory', function ($value, $route) {
if ($value) {
$category = Category::where('title', $value)->first();
if ($category) {
return $category;
}
App::abort(404);
}
});
Route::get('{category}/{subcategory?}', 'CategoriesController@get');
更新:
现在我做了这个,但我认为这不是最好的解决方案。
Route::bind('category', function ($value) {
$category = Category::where('title', $value)->whereNull('parent_id')->first();
if (!$category) {
App::abort(404);
}
Route::bind('subcategory', function ($value, $route) use ($category) {
if ($value) {
$subcategory = Category::where('title', $value)->where('parent_id', $category->id)->first();
if (!$subcategory) {
App::abort(404);
}
return $subcategory;
}
});
return $category;
});
【问题讨论】: