i have some issues that happen with my code for displaying posts by category.. Relation is one to many, post to category.. I can route to choosen category, but posts did not show..
Here's my Post Model:
public function category()
{
return $this->belongsTo(CategoriesPost::class);
}
Category Model:
public function posts()
{
return $this->hasMany(Post::class, 'category_id');
}
And here's my PostController
$posts = Post::where('article_status', 'published')->whereHas('category', function ($query) use ($slug) {
return $query->where('category_slug', $slug);
});
$category = CategoriesPost::where('category_slug', $slug)->first();
return view('pages.article', [
'posts' => $posts,
'category' => $category,
]);
I'm so sure my code was right, but why it didn't shows the posts, did anyone notice where's the problem of my code?