PHP code example of musonza / chat

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];

$conversation = Chat::createConversation($participants);

$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'

$conversation = Chat::conversations()->getById($id);

$data = ['title' => 'PHP Channel', 'description' => 'PHP Channel Description'];
$conversation->update(['data' => $data]);

$message = Chat::message('Hello')
            ->from($model)
            ->to($conversation)
            ->send();

$message = Chat::message('http://example.com/img')
		->type('image')
		->from($model)
		->to($conversation)
		->send();

$message = Chat::message('Attachment 1')
		->type('attachment')
		->data(['file_name' => 'post_image.jpg', 'file_url' => 'http://example.com/post_img.jpg'])
		->from($model)
		->to($conversation)
		->send();

$message = Chat::messages()->getById($id);

$sendModel = $message->sender;

Chat::message($message)->setParticipant($participantModel)->markRead();

Chat::message($message)->setParticipant($participantModel)->toggleFlag();

Chat::message($message)->setParticipant($participantModel)->flagged(); // true

Chat::conversation($conversation)->setParticipant($participantModel)->readAll();

$unreadCount = Chat::messages()->setParticipant($participantModel)->unreadCount();

Chat::conversation($conversation)->setParticipant($participantModel)->unreadCount();

Chat::message($message)->setParticipant($participantModel)->delete();

// Add a reaction
Chat::message($message)->setParticipant($participantModel)->react('👍');

// Add multiple different reactions
Chat::message($message)->setParticipant($participantModel)->react('❤️');

Chat::message($message)->setParticipant($participantModel)->unreact('👍');

$result = Chat::message($message)->setParticipant($participantModel)->toggleReaction('👍');
// $result = ['added' => true/false, 'reaction' => Reaction|null]

$summary = Chat::message($message)->reactionsSummary();
// ['👍' => 5, '❤️' => 3, '😂' => 1]

// Check for specific reaction
Chat::message($message)->setParticipant($participantModel)->hasReacted('👍');

// Check for any reaction
Chat::message($message)->setParticipant($participantModel)->hasReacted();

$reactions = Chat::message($message)->reactions();

$message->reactions; // All reactions
$message->getReactionsSummary(); // Grouped counts
$message->react($participant, '👍'); // Add
$message->unreact($participant, '👍'); // Remove
$message->hasReacted($participant, '👍'); // Check

Chat::conversation($conversation)->setParticipant($participantModel)->clear();

// 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();

Chat::conversations()->setPaginationParams(['sorting' => 'desc'])
	->setParticipant($participantModel)
	->limit(1)
	->page(1)
	->get();

$conversation = Chat::conversations()->between($participantModel1, $participantModel2);

$conversations = Chat::conversations()->common($participants);

/* removing one user */
Chat::conversation($conversation)->removeParticipants([$participantModel]);

/* removing multiple participants */
Chat::conversation($conversation)->removeParticipants([$participantModel, $participantModel2,...,$participantModelN]);

/* add one user */
Chat::conversation($conversation)->addParticipants([$participantModel]);

/* add multiple participants */
Chat::conversation($conversation)->addParticipants([$participantModel, $participantModel2]);

Chat::conversation($conversation)->setParticipant($participantModel)->getMessages()

// 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();

$messages = Chat::conversations()->setParticipant($participantModel)->limit(25)->page(1)->get();

$participants = $conversation->getParticipants();

// Get all models that a user has ever conversed with
$partners = $user->conversationPartners();

Chat::conversation($conversation)->getParticipation($model);

$settings = ['mute_mentions' => true];

Chat::conversation($conversation)
    ->getParticipation($this->alpha)
    ->update(['settings' => $settings]);

/**
 * Model Transformers
 */
'transformers' => [
    'conversation' => \MyApp\Transformers\ConversationTransformer::class,
    'message' => \MyApp\Transformers\MessageTransformer::class,
    'participant' => \MyApp\Transformers\ParticipantTransformer::class,
]

'encrypt_messages' => true,

// 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"

php artisan vendor:publish --provider="Musonza\Chat\ChatServiceProvider"

php artisan migrate