1. Go to this page and download the library: Download faraztanveer/laravel-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/ */
faraztanveer / laravel-chat example snippets
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Faraztanveer\LaravelChat\Traits\HasChatChannels;
class User extends Authenticatable
{
use HasChatChannels;
// Your existing code...
}
return [
// Your participant model (usually User model)
'participant_model' => App\Models\User::class,
// API route customization
'route_prefix' => 'chat', // Routes: /api/chat/*
'route_middleware' => ['auth:sanctum'], // Authentication middleware
];
class User extends Authenticatable
{
use HasChatChannels;
public function getChatDisplayName(): string
{
return $this->full_name ?? $this->name;
}
}
class User extends Authenticatable
{
use HasChatChannels;
public function chatParticipantColumns(): array
{
return ['id', 'name', 'email', 'avatar_url'];
}
}
namespace App\Listeners;
use Faraztanveer\LaravelChat\Events\MessageStored;
use Faraztanveer\LaravelChat\Http\Resources\MessageResource;
use Illuminate\Support\Facades\Log;
class OnChatMessageStored
{
public function handle(MessageStored $event): void
{
// $event->message is the Message model instance
// Log with a resource for consistent structure as used in API responses
Log::debug('Chat message stored', [
'event' => new MessageResource($event->message),
]);
// Add any custom logic here (broadcast, notifications, etc)
}
}
namespace App\Listeners;
use Faraztanveer\LaravelChat\Events\ChatChannelCreated;
use Faraztanveer\LaravelChat\Http\Resources\ChatChannelResource;
use Illuminate\Support\Facades\Log;
class OnChatChannelCreated
{
public function handle(ChatChannelCreated $event): void
{
// $event->channel is the ChatChannel model instance
Log::debug('Chat channel created', [
'event' => new ChatChannelResource($event->channel),
]);
// Custom logic here (notify users, update UI, etc)
}
}