Answer the question
In order to leave comments, you need to log in
How to display posts from subcategories of this category?
I create a regular table of categories, for which you can assign a parent category in the column parent_id
All posts I add to the lower level category (that is, each post has only one category). When I go to the page of this category (specified to the post), I get a list of posts:
$posts = $category->posts();
return view('category.show')->with(compact('category', 'posts'));
public function posts()
{
return $this->hasManyThrough(Post::class, self::class, 'parent_id', 'category_id');
}
count()
will return zero. If the method is posts()
parent_id
replaced by id
, then nothing will be displayed in the parent category, but nothing will be displayed in the subcategories. Is it possible to somehow add a link to this relation for the correct display of posts on child categories? Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question