Create and manage one-on-one conversations between users
Private chats enable direct one-on-one messaging between users in your application. WireChat automatically handles conversation creation, message routing, and participant management.
Create a private conversation between two users using the createConversationWith() method:
use App\Models\User;$currentUser = auth()->user();$otherUser = User::find(2);// Create or retrieve existing conversation$conversation = $currentUser->createConversationWith($otherUser);// Create with an initial message$conversation = $currentUser->createConversationWith( $otherUser, 'Hello! How are you?');
WireChat automatically detects existing conversations between two users and returns the existing conversation instead of creating duplicates.
Send messages in private conversations using the sendMessageTo() method:
// Send to a user (creates conversation if needed)$message = $currentUser->sendMessageTo($otherUser, 'Hi there!');// Send to an existing conversation$message = $currentUser->sendMessageTo($conversation, 'Another message');
Mark conversations as read and track unread messages:
// Mark conversation as read for current user$conversation->markAsRead($currentUser);// Check if conversation has been readif ($conversation->readBy($currentUser)) { // User has read all messages}// Get unread messages$unreadMessages = $conversation->unreadMessages($currentUser);$unreadCount = $conversation->getUnreadCountFor($currentUser);
When both users in a private conversation delete it, WireChat automatically removes it from the database:
// src/Models/Conversation.php:676-680// If all participants have deleted the conversation, force delete itif ($deletedByBothParticipants) { return $this->forceDelete();}
Clear message history without deleting the conversation:
// Clear all messages for current user$conversation->clearFor($currentUser);// or$currentUser->clearConversation($conversation);// Messages are hidden but can reappear if new messages arrive
// Get the other participant in a private chat$peerParticipant = $conversation->peerParticipant($currentUser);$peerUser = $peerParticipant->participantable;// Get current user's participant record$authParticipant = $conversation->participant($currentUser);// Get both participants$allParticipants = $conversation->participants;
Verify user access before showing conversation content:
// Check if user belongs to conversationif ($currentUser->belongsToConversation($conversation)) { // User has access to this conversation}// This check is automatically performed in WireChat components// src/Livewire/Chat/Chat.php:796abort_unless($this->auth->belongsToConversation($this->conversation), 403);
All WireChat Livewire components automatically verify that the authenticated user belongs to the conversation before displaying any data.
// Get the last message$lastMessage = $conversation->lastMessage;// Get conversation update timestamp$lastActivity = $conversation->updated_at;// Update participant's last active timestamp$participant->update(['last_active_at' => now()]);