PHP code example of asignua / filament-chat

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

    

asignua / filament-chat example snippets


use Asignua\FilamentChat\FilamentChatPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->databaseNotifications()
        ->plugin(FilamentChatPlugin::make());
}

FilamentChatPlugin::make()
    // Who can be written to and added to groups. Default: everybody in the users table.
    ->users(fn (Builder $query) => $query->where('is_active', true)->whereHas('roles'))
    // How a person is called. Default: users.name_attribute, then Filament's HasName, then `name`.
    ->userName(fn (User $user): string => "{$user->first_name} {$user->last_name}")
    // A person's avatar URL; null — initials. Default: Filament's avatar provider
    // (HasAvatar::getFilamentAvatarUrl(), the `avatar_url` attribute, then the panel's default provider).
    ->avatarUsing(fn (User $user): ?string => $user->profile_photo_url),

// config/filament-chat.php
'users' => [
    'model' => null,                    // null = auth.providers.users.model
    'name_attribute' => null,           // e.g. 'full_name' — an attribute or accessor
    'search_columns' => ['name'],       // e.g. ['first_name', 'last_name'] — the conversation search
    'broadcast_key' => null,            // e.g. 'ulid' — names the private channel (default: the primary key)
],

'messages' => [
    'page_size' => 30,      // loaded at once and per "Show earlier messages"
    'max_length' => 5000,
],

use Asignua\FilamentChat\Support\References\ReferenceType;

FilamentChatPlugin::make()
    ->references([
        ReferenceType::resource(OrderResource::class),
        ReferenceType::resource(CustomerResource::class)->color('warning'),
    ]),

ReferenceType::make('invoice', Invoice::class)
    ->label('Invoice')
    ->icon(Heroicon::OutlinedDocumentCurrencyDollar)
    ->color('success')
    ->title(fn (Invoice $invoice): string => $invoice->number)
    ->url(fn (Invoice $invoice): ?string => InvoiceResource::getUrl('view', ['record' => $invoice]))
    ->search(fn (string $search) => Invoice::where('number', 'like', "%{$search}%")->limit(20)->get())
    ->searchColumns(['number', 'customer_name'])   // instead of ->search()
    ->visibleUsing(fn (Invoice $invoice): bool => auth()->user()->can('view', $invoice)),

// config/filament-chat.php
'references' => [
    'all_resources' => true,
    'except' => [ActivityLogResource::class, VisitorResource::class],
],

// or on the plugin
->references([
    ReferenceType::allResources()->except([ActivityLogResource::class]),
    ReferenceType::resource(OrderResource::class)->color('warning'), // explicit types win
]),

use Asignua\FilamentChat\Actions\DiscussInChatAction;

protected function getHeaderActions(): array
{
    return [DiscussInChatAction::make()];
}

// lang/vendor/filament-chat/cs/chat.php
return [
    'chat' => 'Chat',
    'new_group' => 'Nová skupina',
    // ...
];

// lang/vendor/filament-chat/en/chat.php
return [
    'chat' => 'Messages',
];

Schema::table('chat_messages', function (Blueprint $table): void {
    $table->foreignId('reply_to_id')->nullable()->constrained('chat_messages')->nullOnDelete();
});

'models' => [
    'conversation' => App\Models\ChatConversation::class, // extends Asignua\FilamentChat\Models\Conversation
    // participant, message, reaction
],
'tables' => [
    'conversations' => 'chat_conversations',
    // participants, messages, reactions
],

use Asignua\FilamentChat\Data\MessageData;
use Asignua\FilamentChat\Services\ChatService;

$chat = app(ChatService::class);
$conversation = $chat->startDirect($me, $colleague);
$chat->send($conversation, $me, MessageData::fromArray([
    'body' => 'Please check @Olga Green',
    'reference_type' => 'order',
    'reference_id' => $order->id,
]));

// A reply: the ulid of a message in the same conversation (a foreign one is dropped).
$chat->send($conversation, $colleague, MessageData::fromArray([
    'body' => 'Done',
    'reply_to' => $message->ulid,
]));

use Asignua\FilamentChat\Policies\ConversationPolicy;

abort_unless(Gate::allows('update', $conversation), 403);            // the creator, still a member
$chat->updateGroup($conversation, GroupData::fromArray($data));

abort_unless(Gate::allows(ConversationPolicy::LEAVE, $conversation), 403);
$chat->leave($conversation, auth()->user());

FilamentChatPlugin::make()->windowComponent(App\Livewire\MyChatWindow::class); // or ui.window_component

use Asignua\FilamentChat\Enums\ChatHook;

FilamentChatPlugin::make()
    ->renderHook(ChatHook::COMPOSER_TOOLS, fn (ChatWindow $window, array $context) => view('my.attach-button'))
    ->renderHook(ChatHook::MESSAGE_MENU, fn (ChatWindow $window, array $context) => view('my.message-menu', ['message' => $context['message']]));

  // AppServiceProvider::register()
  $this->app->resolving(PanelRegistry::class, function (PanelRegistry $registry): void {
      $registry->get('cms', isStrict: false)?->plugin(FilamentChatPlugin::make());
  });
  
bash
php artisan install:broadcasting --reverb
bash
php artisan reverb:start
nginx
  location ^~ /app/ {
      proxy_pass http://reverb:8080;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header Host $host;
      proxy_read_timeout 60s;
  }
  
bash
php artisan vendor:publish --tag=filament-chat-translations
bash
php artisan vendor:publish --tag=filament-chat-migrations   # the 1.0/1.1 migration you already have is kept
php artisan migrate
bash
php artisan filament-chat:install --skill   # links .claude/skills/filament-chat