PHP code example of unseen-codes / chat

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

    

unseen-codes / chat example snippets


// app/Models/User.php
use UnseenCodes\Chat\Traits\Chattable;

class User extends Authenticatable
{
    use Chattable;
}

// routes/web.php — no controller needed with Livewire/Volt pages
Route::get('/chat/{user}', function (User $user) {
    $conversation = auth()->user()->chatWith($user);
    return view('chat', compact('conversation'));
})->middleware('auth');

use UnseenCodes\Chat\Contracts\ChatManagerContract;

$chat = app(ChatManagerContract::class);

// 1-on-1
$conversation = $chat->findOrCreatePrivateConversation($alice, $bob);

// Group
$conversation = $chat->createGroupConversation('Team Chat', $alice, [$bob, $charlie]);

{{-- resources/views/livewire/chat/show.blade.php --}}


use Livewire\Component;
use App\Models\User;
use UnseenCodes\Chat\Contracts\ChatManagerContract;

new class extends Component {

    public $conversation;

    public function mount(User $user): void
    {
        $this->conversation = app(ChatManagerContract::class)
            ->findOrCreatePrivateConversation(auth()->user(), $user);
    }

    public function render()
    {
        return view('livewire.chat.show');
    }
}

// Livewire 4
Route::livewire('/chat/{user}', 'chat.show')->middleware('auth');

// Livewire 3
Route::get('/chat/{user}', \App\Livewire\Chat\Show::class)->middleware('auth');

// Start or resume a 1-on-1 chat
$conversation = $alice->chatWith($bob);

// Create a group
$conversation = $alice->startGroupChat('Dev Team', [$bob, $charlie]);

// Get all conversations (sorted by latest activity)
$conversations = $alice->conversations;

// Total unread count
$unread = $alice->unreadMessageCount();

// Unread in a specific conversation
$unread = $alice->unreadCountIn($conversation);

// Swap any model
'models' => [
    'message' => \App\Models\Chat\Message::class, // extend the base model
],

// Toggle features — disabled = hidden from UI + skipped in logic
'features' => [
    'attachments'      => true,
    'reactions'        => true,
    'read_receipts'    => true,
    'typing_indicator' => true,
    'group_chat'       => true,
    'message_editing'  => true,
],

// Disable broadcasting → falls back to wire:poll (no WebSocket needed)
'broadcasting' => [
    'enabled' => env('CHAT_BROADCASTING_ENABLED', false),
],

use UnseenCodes\Chat\Models\Conversation;
use UnseenCodes\Chat\Contracts\ChatManagerContract;

Broadcast::channel('conversation.{conversationId}', function ($user, $conversationId) {
    $conversation = Conversation::find($conversationId);
    return $conversation
        && app(ChatManagerContract::class)->isParticipant($user, $conversation);
});

// app/Models/Chat/Message.php
namespace App\Models\Chat;

use UnseenCodes\Chat\Models\Message as BaseMessage;

class Message extends BaseMessage
{
    public function isFlagged(): bool
    {
        return (bool) ($this->meta['flagged'] ?? false);
    }
}

'models' => [
    'message' => \App\Models\Chat\Message::class,
],

use UnseenCodes\Chat\Contracts\ChatManagerContract;
use UnseenCodes\Chat\Contracts\MessageServiceContract;

$chat = app(ChatManagerContract::class);
$msgs = app(MessageServiceContract::class);

$conv = $chat->createPrivateConversation($alice, $bob);
$msgs->send($conv, $alice, 'Hey Bob!');
$msgs->send($conv, $bob, 'Hey Alice!');
bash
php artisan vendor:publish --tag=chat-config
bash
php artisan vendor:publish --tag=chat-migrations
php artisan migrate
blade
{{-- resources/views/chat.blade.php --}}
<div class="h-[600px]">
    <livewire:chat-box :conversation="$conversation" />
</div>
bash
php artisan make:volt chat/show --class
bash
php artisan vendor:publish --tag=chat-views
bash
php artisan install:broadcasting