且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel显示帖子上的最后回复

更新时间:2023-01-19 19:13:25

如果您试图获取 topic reply ,则需要急于加载.另外,如果您与 user 有关系,谁也急于加载它.

If you are trying to get topic's replies you need to eager load. Also if you have relation with user who replied eager load it as well.

使用 latest()方法获取最新回复.

Use latest() method for getting latest replies.

仅对于名称 date ,我们具有 select().

public function show($id)
{
    $topics = $topic->with('replies' => function($query) {
        $query->with('user')->select('name', 'created_at')->latest();
    });
    $theme = Theme::with('topics')->find($id);

    return view('themes.theme', compact('topics', 'theme'));
}