Create and manage multi-user group conversations with roles and permissions
Group chats enable multi-user conversations with customizable permissions, roles, and administrative controls. Create communities, teams, or broadcast channels with ease.
use Wirechat\Wirechat\Enums\ParticipantRole;// Check if user is ownerif ($conversation->isOwner($user)) { // User is the group owner}// Check if user is admin (owner or admin)if ($conversation->isAdmin($user)) { // User has admin privileges}// Via participant record$participant = $conversation->participant($user);if ($participant->isOwner()) { // Participant is owner}if ($participant->isAdmin()) { // Participant is admin or owner}
// Add as regular participant$participant = $conversation->addParticipant( $user, ParticipantRole::PARTICIPANT);// Add as admin$participant = $conversation->addParticipant( $user, ParticipantRole::ADMIN);
2
Handle Existing Members
WireChat prevents duplicate participants and handles edge cases:
// If user was previously removed by admin, you can re-add them$participant = $conversation->addParticipant( $user, ParticipantRole::PARTICIPANT, undoAdminRemovalAction: true);
Private and self conversations have automatic participant limits:
// src/Models/Conversation.php:219-233if ($this->isPrivate()) { abort_if( $this->participants()->count() >= 2, 422, 'Private conversations cannot have more than two participants.' );}if ($this->isSelf()) { abort_if( $this->participants()->count() >= 1, 422, 'Self conversations cannot have more than one participant.' );}
$admin = auth()->user();$memberParticipant = $conversation->participant($memberToRemove);// Remove member (requires admin to be admin/owner)$memberParticipant->removeByAdmin($admin);// Check if member was removed by adminif ($memberParticipant->isRemovedByAdmin()) { // Member was removed by an administrator}
use Wirechat\Wirechat\Panel;Panel::make('admin') ->createGroupAction() // Enable group creation ->groups() // Enable groups feature // ... other configuration
// Get all participants$participants = $conversation->participants;// Get all except current user$peerParticipants = $conversation->peerParticipants($currentUser);// Count participants$count = $conversation->participants()->count();// Get participants by role$admins = $conversation->participants() ->whereIn('role', [ParticipantRole::OWNER, ParticipantRole::ADMIN]) ->get();
Messages in groups are broadcast to all participants:
// When a message is sent, it's broadcast to the group channel// src/Livewire/Chat/Chat.php:673broadcast(new MessageCreated($message, $this->panel()->getId()))->toOthers();// Notifications are sent to all participants except senderNotifyParticipants::dispatch($this->conversation, $message, $this->panel);
$authParticipant = $conversation->participant($currentUser);// Regular members can only delete their own messages// Admins can delete any messageif ($message->ownedBy($currentUser) || ($authParticipant->isAdmin() && $conversation->isGroup())) { $message->deleteForEveryone($currentUser);}
// Check if conversation is a groupif ($conversation->isGroup()) { // This is a group conversation $group = $conversation->group;}// Check group ownershipif ($group->isOwnedBy($user)) { // User owns this group}