Support the ongoing development of Laravel.io →
Laravel Database
Last updated 3 months ago.
0

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.

0

Sign in to participate in this thread!

LoadForge

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.