PHP code example of ritechoice23 / laravel-chat-engine

1. Go to this page and download the library: Download ritechoice23/laravel-chat-engine 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/ */

    

ritechoice23 / laravel-chat-engine example snippets


use Ritechoice23\ChatEngine\Traits\CanChat;

class User extends Authenticatable
{
    use CanChat;
}

use Ritechoice23\ChatEngine\Facades\Chat;

// Create a thread
$thread = Chat::thread()->between($userA, $userB)->create();

// Send a message
$message = Chat::message()
    ->from($userA)
    ->to($thread)
    ->text('Hello!')
    ->send();

// React to message
$userB->react($message, '❤️');

// Bookmark message
$userB->saveItem($message);

// Mark as read
$message->markAsReadBy($userB);

// Single file
Chat::message()
    ->from($request->user())
    ->to($thread)
    ->text('Check this out!')
    ->attachUpload($request->file('photo'))
    ->send();

// Multiple files
Chat::message()
    ->from($request->user())
    ->to($thread)
    ->attachUploads($request->file('attachments'))
    ->send();

// View-once (self-destructing)
Chat::message()
    ->from($request->user())
    ->to($thread)
    ->attachUploadViewOnce($request->file('secret'))
    ->send();

// Text
Chat::message()->from($user)->to($thread)->text('Hello')->send();

// Image
Chat::message()->from($user)->to($thread)
    ->image('https://...', 'Caption')->send();

// Video
Chat::message()->from($user)->to($thread)
    ->video('https://...', 'thumb.jpg', 120)->send();

// File
Chat::message()->from($user)->to($thread)
    ->file('https://...', 'doc.pdf', 'application/pdf')->send();

// Location
Chat::message()->from($user)->to($thread)
    ->location(40.7128, -74.0060, 'NYC')->send();

// Contact
Chat::message()->from($user)->to($thread)
    ->contact('John Doe', '+1234567890', '[email protected]')->send();

// Direct (1-on-1, automatically deduplicated)
Chat::thread()->between($userA, $userB)->create();

// Group
Chat::thread()->group('Team Chat')->participants([$u1, $u2, $u3])->create();

// Channel
Chat::thread()->channel('announcements')->create();

// Broadcast
Chat::thread()->broadcast('System Updates')->create();
bash
php artisan vendor:publish --tag="chat-engine-config"
bash
php artisan migrate