Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel在邮件中显示最后的回复_Php_Laravel_Laravel 5_Laravel 5.2_Laravel 5.1 - Fatal编程技术网

Php Laravel在邮件中显示最后的回复

Php Laravel在邮件中显示最后的回复,php,laravel,laravel-5,laravel-5.2,laravel-5.1,Php,Laravel,Laravel 5,Laravel 5.2,Laravel 5.1,我正试图展示最后一封邮件上的回复。但只有它的名字和日期。我试过以下的方法 我认为这篇文章应该做的是看看对这篇文章的回复。按ID对它们进行排序,然后显示第一个的用户名。但它不起作用。 $topic->repress->sortBydesc('id')->first()->user->username} 我也尝试了这一次,我要求在整个论坛的所有答复,并显示了最后一个。它起作用了,但我不是那个和这篇文章有关的人。 {{$allposts->sortBydesc('id')->first()->user

我正试图展示最后一封邮件上的回复。但只有它的名字和日期。我试过以下的方法

我认为这篇文章应该做的是看看对这篇文章的回复。按ID对它们进行排序,然后显示第一个的用户名。但它不起作用。
$topic->repress->sortBydesc('id')->first()->user->username}

我也尝试了这一次,我要求在整个论坛的所有答复,并显示了最后一个。它起作用了,但我不是那个和这篇文章有关的人。
{{$allposts->sortBydesc('id')->first()->user->username}}

有人知道我怎样才能做到这一点吗? 目前,我正在将这两个参数传递到视图中(这两个参数没有问题,它们可以工作,但我认为应该添加一些内容)


如果您试图获取
主题
回复
,请提前感谢

,您需要立即加载。另外,如果您与回复的
用户有关系,也可以加载它

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

只有
name
date
我们有
select()


topic->Responses是一个单独的模型,通过关系连接吗?如果是这样的话,你需要急切地加载那些与主题->主题类似的内容。我将主题传递给视图,如果我执行类似于
{{$topic->repries->count()}
的操作,它会统计与该主题相关的每个回复,因此类似于
$topic->repries->sortBydesc('id')->first()->user->username}
的操作对我来说是有意义的。相信我,我们的关系没有什么问题。我得说这是控制员对吗?因为它不会让我。它给了我一个错误它只是在回复和箭头之间以及单词主题上画了一条红线。
public function show($id)
{


    $topics = Topic::with('theme')->find($id);
    $theme = Theme::with('topics')->find($id);

    return view('themes.theme')->with('topics', $topics)->with('theme', $theme);
}
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'));
}