PHP code example of dominservice / conversations

1. Go to this page and download the library: Download dominservice/conversations library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dominservice / conversations example snippets


'providers' => [
    // Other service providers...
    Dominservice\Conversations\ConversationsServiceProvider::class,
],

'aliases' => [
    // Other aliases...
    'Conversations' => Dominservice\Conversations\Facade\Conversations::class,
]

// Create a new conversation
$conversationId = Conversations::create([$user1Id, $user2Id], null, null, 'Hello!');

// Add a message to a conversation
Conversations::addMessage($conversationId, 'How are you doing?');

// Get all conversations for a user
$conversations = Conversations::getConversations($userId);

// Get messages in a conversation
$messages = Conversations::getMessages($conversationId, $userId);

// Broadcast that a user is typing
ConversationsBroadcasting::broadcastUserTyping($conversationId, $userId, $userName);

// Create a new conversation between users
$conversationId = Conversations::create([$user1Id, $user2Id], null, null, 'Initial message');

// Add a message to an existing conversation
$messageId = Conversations::addMessage($conversationId, 'Hello, how are you?');

// Create a conversation or add to existing one if it exists
$messageId = Conversations::addMessageOrCreateConversation([$user1Id, $user2Id], 'Hello!');

// Get conversation ID between specific users
$conversationId = Conversations::getIdBetweenUsers([$user1Id, $user2Id]);

// Check if a user is part of a conversation
$isUserInConversation = Conversations::existsUser($conversationId, $userId);

// Delete a conversation (for a specific user)
Conversations::delete($conversationId, $userId);

// Get all conversations for a user
$conversations = Conversations::getConversations($userId);

// Get messages in a conversation
$messages = Conversations::getMessages($conversationId, $userId);

// Get only unread messages
$unreadMessages = Conversations::getUnreadMessages($conversationId, $userId);

// Get count of all unread messages for a user
$unreadCount = Conversations::getUnreadCount($userId);

// Get count of unread messages in a specific conversation
$conversationUnreadCount = Conversations::getConversationUnreadCount($conversationId, $userId);

// Mark a message as read
Conversations::markAsRead($conversationId, $messageId, $userId);

// Mark a message as unread
Conversations::markAsUnread($conversationId, $messageId, $userId);

// Mark a message as deleted
Conversations::markAsDeleted($conversationId, $messageId, $userId);

// Mark a message as archived
Conversations::markAsArchived($conversationId, $messageId, $userId);

// Mark all messages in a conversation as read
Conversations::markReadAll($conversationId, $userId);

// Mark all messages in a conversation as unread
Conversations::markUnreadAll($conversationId, $userId);

// Get all users who have read a specific message
$readBy = Conversations::getMessageReadBy($messageId);

// Get all messages in a conversation with their read status for all users
$messagesWithReadStatus = Conversations::getConversationReadBy($conversationId);

// Example of accessing read receipt information
foreach ($messagesWithReadStatus as $message) {
    echo "Message: {$message['content']}\n";
    echo "Read by {$message['read_count']} users\n";

    foreach ($message['read_by'] as $user) {
        echo "- {$user->name} ({$user->email})\n";
    }
}

// Add a reaction to a message
$reactionId = Conversations::addReaction($messageId, '👍');

// Remove a reaction from a message
Conversations::removeReaction($messageId, '👍');

// Get all reactions for a message
$reactions = Conversations::getMessageReactions($messageId);

// Get a summary of reactions (grouped by emoji with count)
$reactionsSummary = Conversations::getMessageReactionsSummary($messageId);

// Check if a message has reactions
$message = ConversationMessage::with('reactions')->find($messageId);
if ($message->hasReactions()) {
    // Message has reactions
}

// Check if a user has reacted to a message with a specific emoji
if ($message->hasUserReaction($userId, '👍')) {
    // User has reacted with thumbs up
}

// Add a message with attachments
$file = $request->file('attachment');
$messageId = Conversations::addMessageWithAttachments($conversationId, 'Check out this file!', [$file]);

// Get attachments for a message
$message = ConversationMessage::with('attachments')->find($messageId);
$attachments = $message->attachments;

// Check if a message has attachments
if ($message->hasAttachments()) {
    $firstAttachment = $message->getFirstAttachment();
    $attachmentUrl = $firstAttachment->getUrlAttribute();

    // For images, get thumbnails
    if ($firstAttachment->isImage()) {
        $thumbnailUrl = $firstAttachment->getThumbnailUrl('small');
    }

    // Check if attachment 

// Edit a message
$updatedMessage = Conversations::editMessage($messageId, 'This is the updated content');

// Check if a message is editable by the current user
$isEditable = Conversations::isMessageEditable($messageId);

// Set whether a message is editable (override the time limit)
Conversations::setMessageEditable($messageId, false); // Disable editing for this message

// Reply to a message
$replyMessageId = Conversations::replyToMessage($parentMessageId, 'This is a reply to the parent message');

// Add a message with a parent ID
$messageId = Conversations::addMessage($conversationId, 'This is a reply', false, false, [], $parentMessageId);

// Check if a message is a reply
$message = ConversationMessage::find($messageId);
if ($message->isReply()) {
    // Message is a reply to another message
    $parentMessage = $message->parent;
}

// Get all replies to a message
$message = ConversationMessage::with('replies')->find($parentMessageId);
if ($message->hasReplies()) {
    $replies = $message->replies;
}

// Get the thread root (the topmost parent in a thread)
$threadRoot = $message->getThreadRoot();

// Create a conversation
$conversationId = conversation_create([$user1Id, $user2Id], null, null, 'Initial message');

// Add a message
$messageId = conversation_add_message($conversationId, 'Hello!');

// Get conversations
$conversations = conversations($userId);

// Get messages
$messages = conversation_messages($conversationId, $userId);

// Mark as read
conversation_mark_as_read($conversationId, $messageId, $userId);
bash
php artisan vendor:publish --provider="Dominservice\Conversations\ConversationsServiceProvider"
bash
php artisan migrate
bash
sudo apt-get install php-sqlite3
bash
sudo yum install php-sqlite3