我有一个博客路线,我想用category_slug展示文章。
Route::get('/blog/{category_slug}/{slug}', [App\Http\Controllers\BlogController::class, 'index'])
->where('category_slug', '[\-_A-Za-z]+')
->where('slug', '[\-_A-Za-z]+');
public function categories_blog()
{
return $this->belongsTo(CategoriesBlog::class, 'category_id');
}
public function blogs()
{
return $this->hasMany(Blog::class);
}有了这种雄辩的关系,就可以很好地工作:
示例:www.mysite.com/blog/first_article
public function index($category_slug, $slug)
{
$blogs = Blog::with('categories_blog')
->where('slug', '=', $slug)
->first();
}使用这种雄辩的关系不起作用:
示例:www.mysite.com/blog/accessories/first_article
public function index($category_slug, $slug)
{
$blogs = Blog::with('categories_blog')
->where('category_slug', '=', $category_slug)
->where('slug', '=', $slug)
->first();
}无法识别与“类别博客”的关系:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_slug' in 'where clause' (SQL: select * from `blogs` where `category_slug` = accessories `slug` = first_article limit 1)我该如何修复它,或者有没有最好的方法来获得它?非常感谢。
发布于 2021-07-12 17:23:44
使用whereHas
$blogs = Blog::with('categories_blog')->whereHas('categories_blog',function ($query)use($category_slug){
$query ->where('category_slug', $category_slug);
})
->where('slug',$slug)
->first();https://stackoverflow.com/questions/68344861
复制相似问题