PHP code example of ab192130 / messages

1. Go to this page and download the library: Download ab192130/messages 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/ */

    

ab192130 / messages example snippets


return [
    'user_model' => 'App\User',

    /**
     * If not set, the package will use getKeyName() on the user_model specified above
     */
    'user_model_primary_key' => null,

    /*
     * This will allow you to broadcast an event when a message is sent
     * Example:
     * Channel: mc-chat-conversation.2,
     * Event: Musonza\Chat\Eventing\MessageWasSent
     */
    'broadcasts' => false,

    /**
     * The event to fire when a message is sent
     * See Musonza\Chat\Eventing\MessageWasSent if you want to customize.
     */
    'sent_message_event' => 'Musonza\Chat\Eventing\MessageWasSent',

    /**
     * Automatically convert conversations with more than two users to public
     */
    'make_three_or_more_users_public' => true,
];



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

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

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

// Create a private conversation
$conversation = Chat::createConversation($participants)->makePrivate();

// Create a public conversation
$conversation = Chat::createConversation($participants)->makePrivate(false);

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

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

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

Chat::message($message)->setUser($user)->markRead();

Chat::message($message)->setUser($user)->toggleFlag();

Chat::message($message)->setUser($user)->flagged(); // true

Chat::conversation($conversation)->setUser($user)->readAll();

$unreadCount = Chat::messages()->setUser($user)->unreadCount();

Chat::conversation($conversation)->setUser($user)->unreadCount();

Chat::message($message)->setUser($user)->delete();

Chat::conversation($conversation)->setUser($user)->clear();

$conversation = Chat::conversations()->between($user1, $user2);

$conversations = Chat::conversations()->common($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]);

Chat::conversation($conversation)->setUser($user)->getMessages()

// private conversations
$conversations = Chat::conversations()->setUser($user)->isPrivate()->get();

// public conversations
$conversations = Chat::conversations()->setUser($user)->isPrivate(false)->get();

// all conversations
$conversations = Chat::conversations()->setUser($user)->get();

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

$users = $conversation->users;

php artisan vendor:publish

php artisan migrate

[
      "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]"
        ]
      ]
    ]