Overview
TheInteractsWithWirechat 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
Usage
Add the trait to your User model:Relationships
conversations()
Establishes a morphToMany relationship between the user and conversations.Returns the conversations relationship
Conversation Management
createConversationWith()
Creates or retrieves a private conversation with another user.The user to create a conversation with
Optional initial message to send
Returns the conversation instance
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.The group name
Optional group description
Optional group cover photo
The panel context
Returns the created group conversation
exitConversation()
Exits a group conversation by marking the user’s participant record as exited.The conversation to exit
Returns
true if successfully exited, false otherwisedeleteConversation()
Deletes a conversation for the current user (soft delete).The conversation to delete
This performs a soft delete. The conversation remains accessible to other participants.
clearConversation()
Clears all messages in a conversation for the current user.The conversation to clear
Messaging
sendMessageTo()
Sends a message to a user or conversation.The recipient user or Conversation instance
The message content to send
Returns the created 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.The conversation to check
Whether to ignore global scopes (default: false)
Returns
true if user is a participanthasConversationWith()
Checks if the user has a private conversation with another user.The other user to check
Returns
true if a conversation existshasDeletedConversation()
Checks if the user has deleted a conversation.The conversation to check
Check if deletion has expired (default: false)
Returns
true if conversation is deletedRole Checks
isAdminIn()
Checks if the user is an admin or owner in a conversation/group.The group or conversation to check
Returns
true if user is admin or ownerisOwnerOf()
Checks if the user is the owner of a conversation/group.The group or conversation to check
Returns
true if user is the ownerAccessors
wirechat_name
Gets the display name for WireChat.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.wirechat_profile_url
Gets the profile URL for WireChat.Unread Messages
getUnreadCount()
Gets the unread message count for the user.Optional specific conversation to check
Returns the unread message count
Complete Example
Related
- WirechatUser Contract - Authorization interface
- Chat Component - Chat UI component
- Chats Component - Conversations list component