I have been using this app for over 2 yrs, all the sudden its giving me this error. Not sure what to do or how to sort it out. Hopefully someone on here can help point me in the right direction.
Error.
ErrorException in MessageService.php line 47:
Trying to get property 'conversation_interlocutors' of non-object
in MessageService.php line 47
-- at HandleExceptions->handleError('8', 'Trying to get property 'conversation_interlocutors' of non-object', '/home/admin/web/365onlinework.com/public_html/app/Services/MessageService.php', '47', array('collection' => object(WebsiteUser))) in MessageService.php line 47
--bat MessageService->App\Services\{closure}(object(WebsiteUser), '118') in compiled.php line 12630
-- at Collection->filter(object(Closure)) in MessageService.php line 48
-- at MessageService->getConversations() in ChatController.php line 22
ok So here is section in my code this is pointing to.
public function getConversations()
{
$websites = Website::orderBy('created_at', 'desc')->get();
$conversations = collect();
foreach ($websites as $website) {
if (auth()->user()->can('view', $website)) {
$this->tenant->connect($website);
$collections = $website->managed_users()
->with(['user.conversation_interlocutors' => function ($query) {
$query
->select('id', 'read', 'initiatorId', 'interlocutorId')
->where('read', 0)
->orWhere('read', 1)
->has('initiator')
->has('interlocutor')
->has('messages')
->with(['flagged', 'interlocutor.website',
'initiator' => function ($i) {
$i->select('id', 'username');
}])
->withCount('messages');
}])->get();
$filtered = $collections->filter(function ($collection) {
return $collection->user->conversation_interlocutors;
});
foreach ($filtered as $filter) {
$conversations->push($filter->user->conversation_interlocutors);
}
}
}
$conversations = $conversations->flatten()->sortBy('createStamp')->values();
return $conversations;
}
what the code is doing is pulling a conversation from the database, if there are new messages. for a managed user.
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!
The Laravel portal for problem solving, knowledge sharing and community building.
The community