我有一个Collection
包含根类别和所有后代的。在我的Category
模型中,我已经确定可以有许多与该类别相关的帖子。我使用以下代码检索类别及其后代:
$category = Category::findOrFail($categoryID);
$categoryAndDescendants = $category->getDescendantsAndSelf();
$categoryAndDescendants
是一个保存模型的Collection
对象。Category
是否可以一次检索所有帖子?
我基本上想做类似的事情:
$posts = $categoryAndDescendants->posts()->orderBy('timestamp', 'DESC');
这将检索该特定集合中所有类别及其后代的所有帖子。
感谢您的帮助,我为糟糕的措辞道歉。