1. Go to this page and download the library: Download musonza/chat 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/ */
musonza / chat example snippets
use Illuminate\Database\Eloquent\Model;
use Musonza\Chat\Traits\Messageable;
class Bot extends Model
{
use Messageable;
}
$participantModel->getParticipantDetails();
public function getParticipantDetailsAttribute()
{
return [
'name' => $this->someValue,
'foo' => 'bar',
];
}
$participants = [$model1, $model2,..., $modelN];
// Create a private conversation
$conversation = Chat::createConversation($participants)->makePrivate();
// Create a public conversation
$conversation = Chat::createConversation($participants)->makePrivate(false);
// Create a direct message
// Make direct conversation after creation
$conversation = Chat::createConversation($participants)->makeDirect();
// Specify intent for direct conversation before creation
$conversation = Chat::makeDirect()->createConversation($participants);
$participants = [$model1, $model2];
// Create a named conversation
$conversation = Chat::createConversation($participants, [], 'general');
// The name is accessible on the conversation
echo $conversation->name; // 'general'
// Check for specific reaction
Chat::message($message)->setParticipant($participantModel)->hasReacted('👍');
// Check for any reaction
Chat::message($message)->setParticipant($participantModel)->hasReacted();
// Archive / unarchive for a single participant
Chat::conversation($conversation)->setParticipant($participantModel)->archive();
Chat::conversation($conversation)->setParticipant($participantModel)->unarchive();
// Or directly on the model
$conversation->archive($participantModel);
$conversation->unarchive($participantModel);
// Default listing excludes archived
Chat::conversations()->setParticipant($participantModel)->get();
// Show only archived conversations (e.g. an "Archive" inbox)
Chat::conversations()->setParticipant($participantModel)->archived()->get();
// Show both archived and non-archived together
Chat::conversations()->setParticipant($participantModel)->withArchived()->get();
// Get only image messages
$messages = Chat::conversation($conversation)
->setParticipant($participant)
->ofType('image')
->getMessages();
// Get only attachment messages
$messages = Chat::conversation($conversation)
->setParticipant($participant)
->ofType('attachment')
->getMessages();
// Also works with cursor pagination
$messages = Chat::conversation($conversation)
->setParticipant($participant)
->ofType('image')
->getMessagesWithCursor();
// Get first page
$messages = Chat::conversation($conversation)
->setParticipant($participantModel)
->setCursorPaginationParams([
'perPage' => 25,
'sorting' => 'asc',
])
->getMessagesWithCursor();
// Get next page using cursor from previous response
$nextCursor = $messages->nextCursor()?->encode();
$moreMessages = Chat::conversation($conversation)
->setParticipant($participantModel)
->setCursorPaginationParams([
'perPage' => 25,
'sorting' => 'asc',
'cursor' => $nextCursor,
])
->getMessagesWithCursor();
// private conversations
$conversations = Chat::conversations()->setParticipant($participantModel)->isPrivate()->get();
// public conversations
$conversations = Chat::conversations()->setParticipant($participantModel)->isPrivate(false)->get();
// direct conversations / messages
$conversations = Chat::conversations()->setParticipant($participantModel)->isDirect()->get();
// all conversations
$conversations = Chat::conversations()->setParticipant($participantModel)->get();
// Filter a participant's conversations by name
$conversations = Chat::conversations()->setParticipant($participantModel)->name('general')->get();
// Filter public conversations by name
$conversations = Chat::conversations()->isPrivate(false)->name('announcements')->get();
// Get all public conversations (no participant Private(false)
->limit(10)
->page(1)
->get();
// Sending works the same - encryption is transparent
$message = Chat::message('Secret message')
->from($user)
->to($conversation)
->send();
// Reading works the same - decryption is automatic
echo $message->body; // "Secret message"