1. Go to this page and download the library: Download zedmagdy/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/ */
zedmagdy / filament-chat example snippets
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use ZEDMagdy\FilamentChat\Traits\HasChats;
class User extends Authenticatable
{
use HasChats;
// ...
}
namespace App\Chat;
use App\Filament\Pages\StaffChatPage;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use ZEDMagdy\FilamentChat\ChatSource;
class StaffChatSource extends ChatSource
{
public function getKey(): string
{
return 'staff';
}
public function getLabel(): string
{
return 'Staff Chat';
}
public function getIcon(): string
{
return 'heroicon-o-chat-bubble-left-right';
}
public function getParticipantModel(): string
{
return User::class;
}
public function getPageClass(): string
{
return StaffChatPage::class;
}
// Optional: filter which users can be added to new conversations
public function getAvailableParticipantsQuery(): Builder
{
return User::query()->where('role', 'staff');
}
// Optional: allow users to create new conversations (default: true)
public function allowsNewConversations(): bool
{
return true;
}
// Optional: enable group chats for this source (default: false)
public function allowsGroupChats(): bool
{
return true;
}
// Optional: customize navigation
public function getNavigationGroup(): ?string
{
return 'Communication';
}
public function getNavigationSort(): ?int
{
return 1;
}
// Optional: customize how participant names are displayed
public function getParticipantDisplayName(\Illuminate\Database\Eloquent\Model $participant): string
{
return $participant->name;
}
// Optional: provide avatar URLs
public function getParticipantAvatarUrl(\Illuminate\Database\Eloquent\Model $participant): ?string
{
return $participant->avatar_url;
}
}
namespace App\Filament\Pages;
use ZEDMagdy\FilamentChat\Pages\ChatSourcePage;
class StaffChatPage extends ChatSourcePage
{
protected static string $chatSourceKey = 'staff';
}
namespace App\Providers\Filament;
use App\Chat\StaffChatSource;
use Filament\Panel;
use Filament\PanelProvider;
use ZEDMagdy\FilamentChat\FilamentChatPlugin;
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
// ... other config
->plugin(
FilamentChatPlugin::make()
->sources([
StaffChatSource::class,
])
);
}
}
// app/Chat/PatientChatSource.php
namespace App\Chat;
use App\Filament\Pages\PatientChatPage;
use App\Models\Patient;
use ZEDMagdy\FilamentChat\ChatSource;
class PatientChatSource extends ChatSource
{
public function getKey(): string
{
return 'patient';
}
public function getLabel(): string
{
return 'Patient Messages';
}
public function getIcon(): string
{
return 'heroicon-o-heart';
}
public function getParticipantModel(): string
{
return Patient::class; // any model with HasChats trait
}
public function getPageClass(): string
{
return PatientChatPage::class;
}
}
// app/Filament/Pages/PatientChatPage.php
namespace App\Filament\Pages;
use ZEDMagdy\FilamentChat\Pages\ChatSourcePage;
class PatientChatPage extends ChatSourcePage
{
protected static string $chatSourceKey = 'patient';
}
declare(strict_types=1);
namespace App\Chat;
use App\Filament\Pages\AllMessagesChatPage;
use ZEDMagdy\FilamentChat\AggregateChatSource;
class AllMessagesAggregateChatSource extends AggregateChatSource
{
public function getKey(): string
{
return 'all-messages';
}
public function getLabel(): string
{
return 'All Messages';
}
public function getIcon(): string
{
return 'heroicon-o-inbox-stack';
}
public function getPageClass(): string
{
return AllMessagesChatPage::class;
}
/**
* @return array<int, string>
*/
public function getSourceKeys(): array
{
return ['staff', 'support'];
}
}
declare(strict_types=1);
namespace App\Filament\Pages;
use ZEDMagdy\FilamentChat\Pages\AggregateChatSourcePage;
class AllMessagesChatPage extends AggregateChatSourcePage
{
protected static string $aggregateKey = 'all-messages';
}
use App\Chat\AllMessagesAggregateChatSource;
use App\Chat\StaffChatSource;
use App\Chat\SupportChatSource;
use ZEDMagdy\FilamentChat\FilamentChatPlugin;
FilamentChatPlugin::make()
->sources([
StaffChatSource::class,
SupportChatSource::class,
])
->aggregates([
AllMessagesAggregateChatSource::class,
])
public function allowsNewConversations(): bool
{
return false; // Users can only see existing conversations
}
use ZEDMagdy\FilamentChat\Models\Conversation;
use ZEDMagdy\FilamentChat\Models\Participant;
use ZEDMagdy\FilamentChat\Models\Message;
// Create a direct conversation
$conversation = Conversation::create([
'source' => 'staff',
'type' => 'direct',
]);
// Add participants
Participant::create([
'conversation_id' => $conversation->id,
'participantable_id' => $user1->id,
'participantable_type' => $user1->getMorphClass(),
]);
Participant::create([
'conversation_id' => $conversation->id,
'participantable_id' => $user2->id,
'participantable_type' => $user2->getMorphClass(),
]);
// Send a message
$message = Message::create([
'conversation_id' => $conversation->id,
'senderable_id' => $user1->id,
'senderable_type' => $user1->getMorphClass(),
'body' => 'Hello!',
]);
// Create a group conversation
$group = Conversation::create([
'source' => 'staff',
'type' => 'group',
'name' => 'Project Team',
]);
// Send a system message (no sender)
Message::create([
'conversation_id' => $group->id,
'body' => 'User1 created the group',
]);
$message = Message::create([
'conversation_id' => $conversation->id,
'senderable_id' => $user->id,
'senderable_type' => $user->getMorphClass(),
'body' => 'Check out this file',
]);
// Add an attachment
$message->addMedia($pathToFile)
->toMediaCollection('chat-attachments');
// Get attachments
$message->getMedia('chat-attachments');
namespace App\Models;
use ZEDMagdy\FilamentChat\Models\Conversation as BaseConversation;
class Conversation extends BaseConversation
{
// Add your custom logic
}
use ZEDMagdy\FilamentChat\Events\MessageSent;
Event::listen(MessageSent::class, function (MessageSent $event) {
// Send a notification, update counters, etc.
$event->message;
$event->message->conversation;
$event->message->senderable;
});