eloquent create call to a member function on null

Posted 3 months ago by paul-a-byford

hey everyone. i'm implementing a team send message function that should accept messages sent from the browser and add them to the database. I've tried using the code below to create a conversation if one does not exist (i.e. for when the very first message is sent), however i get an error message 'call to a member function on null'

// my send message function
 
if(is_null($team->conversation)){
            $team->conversation()->create([
                'type' => 'private',
            ]);
}

$team->conversation->sendMessage($request->message);

it seems my call to $team->conversation->sendMessage($request->message) generates the problem, as if the conversation i created does not exist. but when i check the database i can see the conversation was created and when i retry the same request (with the new conversation created) it works fine and sends the message.

what am i not understanding here?

Please sign in or create an account to participate in this conversation.