Skip to main content

Overview

The InteractsWithWirechat trait provides all essential chat functionality to your User model. It includes methods for creating conversations, sending messages, managing groups, and checking conversation membership.

Trait Location

Wirechat\Wirechat\Traits\InteractsWithWirechat

Usage

Add the trait to your User model:
use Wirechat\Wirechat\Traits\InteractsWithWirechat;

class User extends Authenticatable implements WirechatUser
{
    use InteractsWithWirechat;
}

Relationships

conversations()

Establishes a morphToMany relationship between the user and conversations.
return
MorphToMany
required
Returns the conversations relationship
$user = auth()->user();
$conversations = $user->conversations;

Conversation Management

createConversationWith()

Creates or retrieves a private conversation with another user.
peer
Model
required
The user to create a conversation with
message
string|null
Optional initial message to send
return
Conversation|null
required
Returns the conversation instance
$user = auth()->user();
$peer = User::find(2);

// Create conversation without message
$conversation = $user->createConversationWith($peer);

// Create conversation with initial message
$conversation = $user->createConversationWith($peer, 'Hello!');
If a conversation already exists between the two users, the existing conversation is returned instead of creating a duplicate.

createGroup()

Creates a new group conversation.
name
string
required
The group name
description
string|null
Optional group description
photo
UploadedFile|null
Optional group cover photo
panel
Panel|string|null
The panel context
return
Conversation
required
Returns the created group conversation
$user = auth()->user();

$group = $user->createGroup(
    name: 'Development Team',
    description: 'Team collaboration chat',
    photo: $request->file('photo')
);
This method checks canCreateGroups() permission and aborts with 403 if unauthorized.

exitConversation()

Exits a group conversation by marking the user’s participant record as exited.
conversation
Conversation
required
The conversation to exit
return
bool
required
Returns true if successfully exited, false otherwise
$user = auth()->user();
$conversation = Conversation::find(1);

$user->exitConversation($conversation);

deleteConversation()

Deletes a conversation for the current user (soft delete).
conversation
Conversation
required
The conversation to delete
$user = auth()->user();
$conversation = Conversation::find(1);

$user->deleteConversation($conversation);
This performs a soft delete. The conversation remains accessible to other participants.

clearConversation()

Clears all messages in a conversation for the current user.
conversation
Conversation
required
The conversation to clear
$user = auth()->user();
$conversation = Conversation::find(1);

$user->clearConversation($conversation);

Messaging

sendMessageTo()

Sends a message to a user or conversation.
model
Model
required
The recipient user or Conversation instance
message
string
required
The message content to send
return
Message|null
required
Returns the created message
$user = auth()->user();
$recipient = User::find(2);

// Send to a user (creates/finds conversation automatically)
$message = $user->sendMessageTo($recipient, 'Hello there!');

// Send to an existing conversation
$conversation = Conversation::find(1);
$message = $user->sendMessageTo($conversation, 'Group message');
When sending to a user model, a private conversation is automatically created if it doesn’t exist.

Conversation Queries

belongsToConversation()

Checks if the user is a participant in a conversation.
conversation
Conversation
required
The conversation to check
withoutGlobalScopes
bool
Whether to ignore global scopes (default: false)
return
bool
required
Returns true if user is a participant
$user = auth()->user();
$conversation = Conversation::find(1);

if ($user->belongsToConversation($conversation)) {
    // User is a participant
}

hasConversationWith()

Checks if the user has a private conversation with another user.
user
Model
required
The other user to check
return
bool
required
Returns true if a conversation exists
$user = auth()->user();
$otherUser = User::find(2);

if ($user->hasConversationWith($otherUser)) {
    // Conversation exists
}

hasDeletedConversation()

Checks if the user has deleted a conversation.
conversation
Conversation
required
The conversation to check
checkDeletionExpired
bool
Check if deletion has expired (default: false)
return
bool
required
Returns true if conversation is deleted
$user = auth()->user();
$conversation = Conversation::find(1);

if ($user->hasDeletedConversation($conversation)) {
    // User has deleted this conversation
}

// Check if deletion expired (conversation was updated after deletion)
if ($user->hasDeletedConversation($conversation, checkDeletionExpired: true)) {
    // Deletion has expired
}

Role Checks

isAdminIn()

Checks if the user is an admin or owner in a conversation/group.
entity
Group|Conversation
required
The group or conversation to check
return
bool
required
Returns true if user is admin or owner
$user = auth()->user();
$conversation = Conversation::find(1);

if ($user->isAdminIn($conversation)) {
    // User has admin privileges
}

isOwnerOf()

Checks if the user is the owner of a conversation/group.
entity
Group|Conversation
required
The group or conversation to check
return
bool
required
Returns true if user is the owner
$user = auth()->user();
$group = Group::find(1);

if ($user->isOwnerOf($group)) {
    // User owns this group
}

Accessors

wirechat_name

Gets the display name for WireChat.
$user = auth()->user();
echo $user->wirechat_name; // Returns the user's display name
You can customize this by defining a wirechat_name attribute or method in your User model.

wirechat_avatar_url

Gets the avatar URL for WireChat.
$user = auth()->user();
echo $user->wirechat_avatar_url; // Returns the user's avatar URL

wirechat_profile_url

Gets the profile URL for WireChat.
$user = auth()->user();
echo $user->wirechat_profile_url; // Returns the user's profile URL

Unread Messages

getUnreadCount()

Gets the unread message count for the user.
conversation
Conversation|null
Optional specific conversation to check
return
int
required
Returns the unread message count
$user = auth()->user();

// Get total unread count across all conversations
$totalUnread = $user->getUnreadCount();

// Get unread count for specific conversation
$conversation = Conversation::find(1);
$unread = $user->getUnreadCount($conversation);

Complete Example

use App\Models\User;
use Wirechat\Wirechat\Models\Conversation;

// Create a private conversation
$user = auth()->user();
$friend = User::find(2);
$conversation = $user->createConversationWith($friend, 'Hey!');

// Send a message
$user->sendMessageTo($friend, 'How are you?');

// Create a group
$group = $user->createGroup(
    name: 'Project Team',
    description: 'Team collaboration'
);

// Check permissions
if ($user->isOwnerOf($group)) {
    // Perform owner-only actions
}

// Get unread count
$unread = $user->getUnreadCount();

// Delete conversation
$user->deleteConversation($conversation);