Support the ongoing development of →
Laravel Database
Last updated 2 years ago.

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)) {

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!


Your banner here too?


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.

© 2024 - All rights reserved.