使用 "slugs" 通过另一个 table ID 获取项目
Get Items by another table ID using "slugs"
我有一个非常简单的数据库模型,由两个表 Items 和 Categories 组成。
项Table
ID I 标题 I Category_id
类别Table
ID I Name I Slug
此时我可以使用 www.myapp.com/tags/1 等特定类别过滤项目。
Route::get('tags/{category?}', array('as'=>'itemstag', 'uses'=>'ItemsController@show_items'));
这就是控制器的样子。
public function show_items($category = NULL)
{
if($category == NULL)
return View::make('items/index', ['items' => Item::where('publishtime', '<', date('Y-m-d H:i:s'))->orderBy('created_at','Desc')->paginate(24),'categories' => Category::all()]);
else
return View::make('items/index', ['items' => Item::where('publishtime', '<', date('Y-m-d H:i:s'))->orderBy('created_at','Desc')->where('category_id','=',$category)->paginate(24),'categories' => Category::all()]);
}
问题是我想让我的 url 更多 "pretty" 但是如果我将 slug 传递给控制器然后我无法评估条件因为我的项目模型上没有 $slug。我应该在这里加入吗?在这种情况下,实现 slug 的正确方法是什么?
$items = Item::where('publishtime', '<', date('Y-m-d H:i:s'))
->orderBy('created_at','Desc')
->whereHas('category', function ($q) use ($category){
$q->where('slug','=',$category);
})
->paginate(24);
我有一个非常简单的数据库模型,由两个表 Items 和 Categories 组成。
项Table
ID I 标题 I Category_id
类别Table
ID I Name I Slug
此时我可以使用 www.myapp.com/tags/1 等特定类别过滤项目。
Route::get('tags/{category?}', array('as'=>'itemstag', 'uses'=>'ItemsController@show_items'));
这就是控制器的样子。
public function show_items($category = NULL)
{
if($category == NULL)
return View::make('items/index', ['items' => Item::where('publishtime', '<', date('Y-m-d H:i:s'))->orderBy('created_at','Desc')->paginate(24),'categories' => Category::all()]);
else
return View::make('items/index', ['items' => Item::where('publishtime', '<', date('Y-m-d H:i:s'))->orderBy('created_at','Desc')->where('category_id','=',$category)->paginate(24),'categories' => Category::all()]);
}
问题是我想让我的 url 更多 "pretty" 但是如果我将 slug 传递给控制器然后我无法评估条件因为我的项目模型上没有 $slug。我应该在这里加入吗?在这种情况下,实现 slug 的正确方法是什么?
$items = Item::where('publishtime', '<', date('Y-m-d H:i:s'))
->orderBy('created_at','Desc')
->whereHas('category', function ($q) use ($category){
$q->where('slug','=',$category);
})
->paginate(24);