- 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
From the command line, run:
composer require musonza/chat
Add the service provider to your config\app.php the providers array
Musonza\Chat\ChatServiceProvider::class
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.
[
'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,
];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::conversations()->getById($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::messages()->getById($id);Chat::message($message)->for($user)->markRead();Chat::message($message)->for($user)->toggleFlag();
Chat::message($message)->for($user)->flagged(); // trueChat::conversation($conversation)->for($user)->readAll();$unreadCount = Chat::messages()->for($user)->unreadCount();Chat::conversation($conversation)->for($user)->unreadCount();Chat::message($message)->for($user)->delete();Chat::conversation($conversation)->for($user)->clear();$conversation = Chat::conversations()->between($user1, $user2);$conversations = Chat::conversations()->common($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::conversation($conversation)->removeParticipants($user);/* removing multiple users */
Chat::conversation($conversation)->removeParticipants([$user1, $user2, $user3,...,$userN]);/* add one user */
Chat::conversation($conversation)->addParticipants($user);/* add multiple users */
Chat::conversation($conversation)->addParticipants([$user3, $user4]);Note: A third user will classify the conversation as not private if it was.
Chat::conversation($conversation)->for($user)->getMessages()$messages = Chat::conversations()->for($user)->limit(25)->page(1)->get();Example
[
"id" => 1
"private" => "1"
"data" => []
"created_at" => "2018-06-02 21:35:52"
"updated_at" => "2018-06-02 21:35:52"
"last_message" => array:13 [
"id" => 2
"message_id" => "2"
"conversation_id" => "1"
"user_id" => "1"
"is_seen" => "1"
"is_sender" => "1"
"flagged" => false
"created_at" => "2018-06-02 21:35:52"
"updated_at" => "2018-06-02 21:35:52"
"deleted_at" => null
"body" => "Hello 2"
"type" => "text"
"sender" => array:7 [
"id" => 1
"name" => "Jalyn Ernser"
"email" => "[email protected]"
]
]
]
There are a few ways you can achieve pagination
You can specify the limit and page as above using the respective functions or as below:
$paginated = Chat::conversations()->for($user)
->setPaginationParams([
'page' => 3,
'perPage' => 10,
'sorting' => "desc",
'columns' => [
'*'
],
'pageName' => 'test'
])
->get();
You don't have to specify all the parameters. If you leave the parameters out, default values will be used.
$paginated above will return Illuminate\Pagination\LengthAwarePaginator
To get the conversations simply call $paginated->items()
$users = $conversation->users;Chat is open-sourced software licensed under the MIT license