Latest

Monday, July 10, 2017

Call to undefined relationship [topic] on model [App\Theme]

Asked by: Feudelcosine148


I'm trying to display the latest Topic that is related to a Theme. I did this before and I tried the following code in the ThemesController (Index method, Sinse it's the homepage),

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

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

And the method I'm calling is this.

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

The method sits inside the Theme Model, It doesn't matter if i put the method in the Topic Model of the Theme Model, It still gives me the same error. So what is causing this issue? This is for my homepage so the route is this Route::get('/', 'ThemesController@index')->name('home');. I know it is something I've done before but I can't figure it out. Thanks in advance


Answers

Answered by: rap-2-h at 2017-07-10 06:23PM



You should add a topic method in your Theme model (if you haven't already did).

Something like:

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



Source

No comments:

Post a Comment

Adbox