按最新(最新)相关型号订购[Laravel]

问题描述 投票:1回答:2

我正在使用Laravel 5.4,我有以下关系:

  • 论坛hasMany线程(threads()
  • 线程hasMany帖子(posts()
  • 线程belongsTo用户(user()
  • 发布belongsTo用户(user()

目前,在我的ThreadsController@index,我有以下内容:

public function index()
{
    $threads = $this->forum->threads()
        ->approved()
        ->withCount(['posts AS approved_replies' => function ($query) {
            $query->where('posts.approved', true)->where('posts.is_starting_thread', false);
        }])
        ->with(['posts' => function ($query) { // Posts
            $query->approved()
                ->with('user') // Author of post
                ->latest();
            }]
        )
        ->with('user') // Author of thread
        ->latest()
        ->paginate(20); 

    return view('forums.threads.index')->with([
        'forum' => $this->forum, 'threads' => $threads
    ]);
}

我的index.blade.php应该显示线程列表,其中每个线程将有:

  • 它的作者(这就是为什么我有->with('user')
  • 回复的数量(这就是为什么我有>withCount(['posts AS approved_replies' => function ($query) { ...
  • 最新(最新)帖子及其作者的日期。这就是为什么: ->with(['posts' => function ($query) { // Posts $query->approved() ->with('user') // Author of post ->latest(); // LATEST first }] ) ...因为在index.blade.php中我可以通过以下方式访问每个线程的最新帖子: @foreach ($threads as $thread) {{ $thread->posts->first()->created_at; }} {{ $thread->posts->first()->user->username; }} @endforeach

这段代码的问题在于线程按照created_at排序,而不是最近的帖子。我想要实现的是通过最新(最新)的帖子订购线程,但我不知道如何做到这一点。

php sql laravel laravel-5 laravel-eloquent
2个回答
4
投票

这是我怎么做的。这不是特别漂亮,但应该做的工作

$this->forum->threads()
    ->approved()
    ->join('posts', 'posts.thread_id', '=', 'threads.id')
    ->selectRaw('threads.*, MAX(posts.created_at) AS latest_post_at')
    ->groupBy('threads.id')
    ->orderByDesc('latest_post_at')
    ->withCount(['posts AS approved_replies' => function ($query) {
        $query->where('posts.approved', true)->where('posts.is_starting_thread', false);
    }])
    ->with(['posts' => function ($query) { // Posts
        $query->approved()
            ->with('user') // Author of post
            ->latest();
        }]
    )
    ->with('user')
    ->paginate(20);

它做了一个join作为另一个答案建议,然后groups线程,使用MAX聚合函数来保持每组帖子的最新日期。

Edit:

$this->forum->threads()
    ->approved()
    ->join('posts', 'posts.thread_id', '=', 'threads.id')
    ->select('threads.*', 'posts.created_at AS latest_post_at')
    ->whereNotExists(function ($subquery) {
        return $subquery->from('posts AS later_posts')
            ->whereRaw('later_posts.thread_id = posts.thread_id')
            ->whereRaw('later_posts.created_at > posts.created_at');
    })
    ->orderByDesc('latest_post_at')
    ->withCount(['posts AS approved_replies' => function ($query) {
        $query->where('posts.approved', true)
              ->where('posts.is_starting_thread', false);
    }])
    ->with(['posts' => function ($query) { // Posts
        $query->approved()
            ->with('user') // Author of post
            ->latest();
    }])
    ->with('user')
    ->paginate(20);

0
投票

你需要使用join()

$threads = Thread::join('posts as p', 'p.thread_id', '=', 'threads.id')
    ->where('forum_id', $this->forum->id)
    ->approved()
    ->withCount(['posts AS approved_replies' => function ($query) {
        $query->where('posts.approved', true)->where('posts.is_starting_thread', false);
    }])
    ->with(['posts' => function ($query) { // Posts
        $query->approved()
            ->with('user') // Author of post
            ->latest();
        }]
    )
    ->with('user') // Author of thread
    ->latest('p.created_at')
    ->paginate(20); 
© www.soinside.com 2019 - 2024. All rights reserved.