mage95 / tbmsg

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel User Messaging system

PHP multi user multi conversations messaging system, just like facebook :)

** just wanted to thank everyone for the feedback, and great bugs! I'm still working hard to improve it, and I wanted to invite you all to the website that is the reason for me to create this package, and of course you could test it there:

Currently hapening:

I noticed many people like this package so I now am upgrading it with tests and better structure, anyone who has any ideas or wants to contribute I would be more than happy :)


I decided to create this package because of the lack of real good messaging systems in php with the proper features like, deleting archiving messages for each specific user, multipile users in conversations and more. I am trying to build this in a very generic way so it could be easilly implemented for each framework or project. Currently I'll try to build with laravel and eloquent as this is the project I'm currently building.


  • Message status for each user: deleted, read, unread, archived
  • multipile conversations for each user
  • each conversation has 2 or more users
  • ability to have 1 unique conversation for each 2 users
  • Get all users conversations with last message in conversation
  • get all conversations messages for a specific user

How it is built:

      users: id

      messages: id, sender_id, conv_id, content, updated_at, created_at

      messages_status: id, user_id, msg_id, self, status

      conversations: id, updated_at, created_at

      conv_users: conv_id, user_id


simply hold the users that use the messages


contains the message itself, who sent it, to which conversation, the content and the dates.


this is the "magic" it has a row for each user that got a message in a specific conversation. IE if I send a message to conversation with 4 users, we will have 5 rows in this table, the sender and the other 4 recipents, it will contains the specific user message status, as is it deleted, read, unread...


currently only has an id, and dates


holds the users in conversation relationships


  1. Add this: '"tzookb/tbmsg": "1.0.*"' to your composer.json file
  2. run: "composer update tzookb/tbmsg"
  3. Now add the service provider to your app.php file: "'Tzookb\TBMsg\TBMsgServiceProvider'"
  4. It would be best if you register the Facade in your app.php file: "'TBMsg' => 'Tzookb\TBMsg\Facade\TBMsg'"
  5. run the migrations: "php artisan migrate --package=tzookb/tbmsg"

How to use it?:

Get User Conversations:

        $convs = TBMsg::getUserConversations($user_id);

This will return you a "Illuminate\Support\Collection" of "Tzookb\TBMsg\Entities\Conversation" objects. And foreach Conversation there, you will have the last message of the conversation, and the participants of the conversation. Example:

        foreach ( $convs as $conv ) {
            $getNumOfParticipants = $conv->getNumOfParticipants();
            $participants = $conv->getAllParticipants();
            /* $lastMessage Tzookb\TBMsg\Entities\Message */
            $lastMessage = $conv->getLastMessage();
            $senderId = $lastMessage->getSender();
            $content = $lastMessage->getContent();
            $status = $lastMessage->getStatus();

Get User specific conversation:

        $conv = TBMsg::getConversationMessages($conv_id, $user_id);

This will return you a "Tzookb\TBMsg\Entities\Conversation" object. On the object you could get all messages, all participants, conv_id, and more, simply browse the object itself. Example:

        foreach ( $conv->getAllMessages() as $msg ) {
            $senderId = $msg->getSender();
            $content = $msg->getContent();
            $status = $msg->getStatus();

Get the conversation id of a conversation between two users:

        $conv = TBMsg::getConversationByTwoUsers($userA_id, $userB_id);

Simply gives you an id of the conversation between two users, this was created for redirecting to the conversation page when user tries to send a message to another user, so if there is no id returned that means that those users has no conversation yet, so we could create one.

Add a new message to conversation:

        $conv = TBMsg::addMessageToConversation($conv_id, $user_id, $content);

Simply add a message to an exiting conversation, content is the message text.

Create a new conversation:

        $conv = TBMsg::createConversation($users_ids=array());

Creates a new conversation with the users id's you passed in the array.

Get all users in conversation:

        $conv = TBMsg::getUsersInConversation($conv_id);

returns an array of user id in the conversation.

Delete conversation:

        $conv = TBMsg::deleteConversation($conv_id, $user_id);

"Deletes" the conversation from a specifc user view.

Check if user is in conversation:

        $conv = TBMsg::isUserInConversation($conv_id, $user_id);

True or False if user is in conversation.

Get number of unread messages for specific user:

        $conv = TBMsg::getNumOfUnreadMsgs($user_id);

return an integer of number of unread messages for specific user.

Mark all messages as "read" for specifc user in conversation:

        $conv = TBMsg::markReadAllMessagesInConversation($conv_id, $user_id);


        public function conversations($convId=null) {
            $currentUser = Auth::user();
            //get the conversations
            $convs = TBMsg::getUserConversations( $currentUser->id );
            //array for storing our users data, as that Tbmsg only provides user id's
            $participants = [];
            //gathering participants
            foreach ( $convs as $conv ) {
                $participants = array_merge($participants, $conv->getAllParticipants());
            //making sure each user appears once
            $participants = array_unique($participants);
            //getting all data of participants
            $viewUsers = [];
            if ( !empty($participants) ) {
                $users = User::whereIn('id', $participants)->with('profileImage')->getDictionary();
            return View::make('conversations_page')
                ->with('users', $users)
                ->with('user', $currentUser)
                ->with('convs', $convs);



Language:PHP 100.0%