在模型[App \ Theme]上调用未定义的关系[主题]

时间:2017-07-10 12:49:40

标签: php laravel laravel-5 laravel-5.2 laravel-5.1

我试图显示与主题相关的最新主题。我之前做过这个,我在ThemesController中尝试了以下代码(索引方法,Sinse它是主页),

$topics = Theme::with('topic', 'lastTopic.user')->get();
$themes = Theme::all();

return view('themes.index')->with('themes', $themes)->with('topics', $topics);

我打电话的方法就是这个。

public function lastTopic()
{
    return $this->hasOne(Topic::class)->latest();
}

该方法位于主题模型中,如果我将该方法放在主题模型的主题模型中并不重要,它仍然给我同样的错误。那导致这个问题的原因是什么?这是我的主页,所以路线是Route::get('/', 'ThemesController@index')->name('home');。我知道这是我以前做过的事情,但我无法弄明白。提前致谢

1 个答案:

答案 0 :(得分:1)

您应该在topic模型中添加Theme方法(如果您还没有)。

类似的东西:

public function topic()
{
    return $this->hasMany(Topic::class);
}
相关问题