Skip to main content
Version: Next

Using Conversation

Creating conversation#

When conversation is created, the system will automatically add the creator as a participant of the conversation using the conversation.user_id if only the chatSystem Observer have been registered. see registering-observers

$conversation = $user->conversations()->create([  'user_id' => $user->id,]);
output
// conversation{  "id": 297,  "user_id": 13,  "type": "private",  "updated_at": "2021-07-14T18:59:44.000000Z",  "created_at": "2021-07-14T18:59:44.000000Z"}

Creating conversation type#

You may create a conversation of another type such as group and should have a name.

$conversation = $user->conversations()->create([  'user_id' => $user->id,  'type'    => 'group',  'name'    => 'Laravel Developer\'s Group',]);
output
// conversation{  "id": 297,  "user_id": 13,  "type": "group",  "name": "Laravel Developer\'s Group",  "updated_at": "2021-07-14T18:59:44.000000Z",  "created_at": "2021-07-14T18:59:44.000000Z"}

Adding/removing user/participant to conversation#

You may add as many participants to a conversation but its not a good idea for a conversation of type private to have more than 2 participants. The function will also create a message of type = activity. pass a message argument to customise the activity message.

$conversation->addParticipant($user, message: 'Someone joined the conversation');$conversation->removeParticipant($user, message: 'Someone left the conversation');

Deleting conversation#

You may delete conversation with makeDelete method which requires 1 argument = user deleting the conversation. You can specify delete for all option by passing named argument all which will specify that the conversation has been deleted for all participants. The method will also try to emit Message Events

$conversation->makeDelete($user, all: true);