Laravel 4 如何使用Laravel和Elotent列出特定论坛类别中的最新回复

Laravel 4 如何使用Laravel和Elotent列出特定论坛类别中的最新回复,laravel-4,eloquent,Laravel 4,Eloquent,我正在一个论坛上工作。我在Laravel中有以下模型结构,且雄辩: Category hasMany Threads Thread hasMany Posts Thread hasManyThrough Reply, Post Post hasMany Replies 我可以使用以下方法列出一篇文章的最新5条回复: $recentReplies = $post->replies()->orderBy('created_at', 'desc')->take(5)->get

我正在一个论坛上工作。我在Laravel中有以下模型结构,且雄辩:

Category hasMany Threads
Thread hasMany Posts
Thread hasManyThrough Reply, Post
Post hasMany Replies
我可以使用以下方法列出一篇文章的最新5条回复:

$recentReplies = $post->replies()->orderBy('created_at', 'desc')->take(5)->get();
我甚至可以使用hasManyThrough relation列出针对一个线程的最新5条回复,使用:

$recentReplies = $thread->replies()->orderBy('created_at', 'desc')->take(5)->get();
我的问题是如何列出一个类别的最新5条回复?

以下内容不起作用:

$recentReplies = $category->threads()->replies()->orderBy('created_at', 'desc')->take(5)->get();
我也尝试了以下方法,但失败了:

$recentReplies = $category->threads->replies()->orderBy('created_at', 'desc')->take(5)->get();
我有以下示例模型结构:

class Category extends Eloquent {

    public function threads()
    {
        return $this->hasMany('Thread');
    }

}

class Thread extends Eloquent {

    public function posts()
    {
        return $this->hasMany('Post');
    }

    public function replies()
    {
        return $this->hasManyThrough('Reply', 'Post');
    }

}

class Post extends Eloquent {

    public function replies()
    {
        return $this->hasMany('Reply');
    }

}

class Reply extends Eloquent {
    // NOTE: belongsTo method exists in all models
}
提前感谢您花费的时间和精力

问候

Ahmed Khan使用以下技巧:

$recentReplies = null;

// let's find 5 replies for category with $categoryId
Category::with(['threads.replies' => function ($q) use (&$recentReplies) {

   $recentReplies = $q->latest()->take(5)->get();
}])->find($categoryId);

谢谢你,Jarek,我在你的代码样本中用线程替换了thred,这个技巧确实奏效了,解决了我的问题。很抱歉,回复太晚,因为我的邮箱中没有收到任何关于此回复的通知。不知道为什么。我的问题现在解决了。