dimanche 24 mars 2019

How to know when my relation returns an object and when an array?

I got few relations in my model Reply:

public function thread(){

    return $this->belongsTo(Thread::class);
}

public function user(){

    return $this->belongsTo(User::class);
}

My reply table got user_id and thread_id

And then i try to get user name i do it like this:

$reply->user->name

And it works.

But when i try to get thread title:

$reply->thread->title

I got error trying to get property 'title' of non-object and if need to get title only method i know is:

$reply->thread['title']

What is the difference between methods i use? Why in one case i get user as an object but in another i get thread as an array?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire