/chat

A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

Primary LanguagePHP

Downloads StyleCI

Chat

Introduction

This package allows you to add a chat system to your Laravel ^5.3 application

Note: If you are using a Laravel version less than 5.3 install the release on this branch instead.

Installation

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.

Run the migrations:

php artisan migrate

Usage

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.

Creating a conversation

$participants = [$userId, $userId2,...];

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

Get a conversation by id

$conversation = Chat::conversation($conversation_id);

Send a text message

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

Send a message of custom type

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

Mark a message as read

Chat::messages($message)->for($user)->markRead();

Mark whole conversation as read

Chat::conversations($conversation)->for($user)->readAll();

Delete a message

Chat::messages($message)->for($user)->delete();

Clear a conversation

Chat::conversations($conversation)->for($user)->clear();

Get a conversation between two users

Chat::getConversationBetween($user1, $user2);

Remove users from a conversation

/* removing one user */
Chat::removeParticipants($conversation, $user);
/* removing multiple users */
Chat::removeParticipants($conversation, [$user1, $user2, $user3,...,$userN]);

Add users to a conversation

/* 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.

Get messages in a conversation

Chat::conversations($conversation)->for($user)->getMessages($perPage, $page)

Get recent messages

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

Get users in a conversation

$users = $conversation->users;

License

Chat is open-sourced software licensed under the MIT license