- Introduction
- Installation
- Usage
- Creating a conversation
- Get a conversation by Id
- Update conversation details
- Send a text message
- Send a message of custom type
- Get a message by id
- Mark a message as read
- Mark whole conversation as read
- Unread messages count
- Delete a message
- Clear a conversation
- Get a conversation between two users
- Get common conversations among users
- Remove users from a conversation
- Add users to a conversation
- Get messages in a conversation
- Get recent messages
- Get users in a conversation
- License
This package allows you to add a chat system to your Laravel ^5.4 application
Note: If you are using a Laravel version less than 5.4 install the release on this branch instead.
From the command line, run:
composer require musonza/chat
Add the service provider to your config\app.php
the providers array
Musonza\Chat\ChatServiceProvider
Add the Facade to your aliases:
'Chat' => Musonza\Chat\Facades\ChatFacade::class to your `config\app.php`
The class is bound to the ioC as chat
$chat = App::make('chat');
Publish the assets:
php artisan vendor:publish
This will publish database migrations and a configuration file musonza_chat.php
in the Laravel config folder.
Note: This package takes advantage of Laravel Notifications. If you have already setup Laravel notifications you can delete the
2017_07_12_034227_create_notifications_table.php
migration file.
[
'user_model' => 'App\User',
/**
* This will allow you to broadcast an event when a message is sent
* Example:
* Channel: private-mc-chat-conversation.2,
* Event: Musonza\Chat\Messages\MessageWasSent
*/
'broadcasts' => false,
/**
* If set to true, this will use Laravel notifications table to store each
* user message notification.
* Otherwise it will use mc_message_notification table.
* If your database doesn't support JSON columns you will need to set this to false.
*/
'laravel_notifications' => true,
];
Run the migrations:
php artisan migrate
By default the package assumes you have a User model in the App namespace.
However, you can update the user model in musonza_chat.php
published in the config
folder.
$participants = [$userId, $userId2,...];
$conversation = Chat::createConversation($participants);
$conversation = Chat::conversation($conversation_id);
$data = ['title' => 'PHP Channel', 'description' => 'PHP Channel Description'];
$conversation->update(['data' => $data]);
$message = Chat::message('Hello')
->from($user)
->to($conversation)
->send();
The default message type is text
. If you want to specify custom type you can call the type()
function as below:
$message = Chat::message('http://example.com/img')
->type('image')
->from($user)
->to($conversation)
->send();
$message = Chat::messageById($id);
Chat::messages($message)->for($user)->markRead();
Chat::conversations($conversation)->for($user)->readAll();
$unreadCount = Chat::for($user)->unreadCount();
Chat::messages($message)->for($user)->delete();
Chat::conversations($conversation)->for($user)->clear();
Chat::getConversationBetween($user1, $user2);
$conversations = Chat::commonConversations($users);
$users
can be an array of user ids ex. [1,4,6]
or a collection (\Illuminate\Database\Eloquent\Collection)
of users
/* removing one user */
Chat::removeParticipants($conversation, $user);
/* removing multiple users */
Chat::removeParticipants($conversation, [$user1, $user2, $user3,...,$userN]);
/* add one user */
Chat::addParticipants($conversation, $user);
/* add multiple users */
Chat::addParticipants($conversation, [$user3, $user4]);
Note: A third user will classify the conversation as not private if it was.
Chat::conversations($conversation)->for($user)->getMessages($perPage, $page)
$messages = Chat::conversations()->for($user)->limit(25)->page(1)->get();
$users = $conversation->users;
Chat is open-sourced software licensed under the MIT license