Reputation: 89
$conversations = Conversations::with('bulletin', 'messages')->leftJoin('own', 'conversations.bulletin_id', '=',
'own.id')
->select('conversations.*')
->where('owner_id', $userId)
->orWhere('owner', $userId)
->orderBy('updated_at', 'desc')
->get();
dd($conversations);
$conversation = Conversations::getInfo($conversations->first());
foreach ($conversations as $conversation) {
dd($conversation->bulletin);
}
https://i.sstatic.net/Up1dy.png - screenshot with first dd()
https://i.sstatic.net/hGAHg.png - screenshot dd() in foreach() - nothing here
If I try to access to $conversation->bulletin->id
this will be error 'undefined property...'
public static function getInfo($conversation)
{
$conversation->messages = self::find($conversation->id)->messages();
$conversation->bulletin = self::find($conversation->id)->bulletin();
$conversation->users = self::find($conversation->id)->users();
return $conversation;
}
Upvotes: 0
Views: 57
Reputation: 4750
This happening because for some $conversations
or may be one of the $conversation
doesn't have any bulletin
. As a result when you loop through all the $conversations
and access $conversion->bulletin->id
that one conversation
cause the error. When you run dd
it only dump the first conversation
and die. As a result you are not seeing correctly what is the problem.
replace the following line:
dd($conversation->bulletin);
with this line and see:
dump($conversation->id, $conversation->bulletin);
I am quite sure you will see at least one conversation doesn't have any bulletin.
Update:
self::find($conversation->id)->users(); will return the relationship object, not the collection of users. If you want access on collection of user then you should do self::find($conversation->id)->users
. Same for bulletin
and messages
Actually, I don't think you need this getInfo()
method. You just need to use the with
method of query properly,
So, replace:
Conversations::with('bulletin','messages')
this part by Conversations::with('bulletin','messages',
users)
Upvotes: 1