You don't specify which line is actually throwing the error, but it probably either the collection-filter or the foreach loop.
In either of those two lines, conversation_interlocutors doesn't exist. That means the user may or may not have a conversation attached? Maybe a message was deleted 'the wrong way' so your system crashes?
Maybe the easy solution is to just check if the conversation_interlocutors exist before returning and pushing them:
$filtered = $collections->filter(function ($collection) {
if (isset($collection->user->conversation_interlocutors)) {
return $collection->user->conversation_interlocutors;
}
});
foreach ($filtered as $filter) {
if (isset($filter->user->conversation_interlocutors)) {
$conversations->push($filter->user->conversation_interlocutors);
}
}
That way, you're making sure you are only working with existing data, even if the database has been corrupted somehow.
Sign in to participate in this thread!