PHP code example of muba00 / laravel-live-chat

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

    

muba00 / laravel-live-chat example snippets


use muba00\LaravelLiveChat\Models\Conversation;
use muba00\LaravelLiveChat\Models\Message;

// Get or create a conversation between two users
$conversation = Conversation::between(auth()->user(), $otherUser);

// Send a message
$message = Message::create([
    'conversation_id' => $conversation->id,
    'sender_id' => auth()->id(),
    'message' => 'Hello! How are you?',
]);

return [
    // User model configuration
    'user_model' => 'App\\Models\\User',

    // Customize table names
    'tables' => [
        'conversations' => 'live_chat_conversations',
        'messages' => 'live_chat_messages',
    ],

    // API routes configuration
    'routes' => [
        'enabled' => true,
        'prefix' => 'chat/api',
        'middleware' => ['api', 'auth:sanctum'],
    ],

    // Broadcasting settings
    'broadcasting' => [
        'enabled' => true,
        'channel_prefix' => 'chat',
    ],

    // Pagination
    'pagination' => [
        'messages_per_page' => 50,
        'conversations_per_page' => 20,
    ],

    // Message retention and storage
    'storage' => [
        'retention_days' => null, // null = keep forever
        'archive_enabled' => false,
        'max_message_length' => 5000,
    ],

    // Feature toggles
    'features' => [
        'read_receipts' => true,
        'typing_indicators' => true,
    ],

    // Security settings
    'security' => [
        'sanitize_messages' => true,
        'max_conversations_per_user' => 100,
    ],
];

use muba00\LaravelLiveChat\Models\Conversation;
use muba00\LaravelLiveChat\Models\Message;

// Get or create a conversation
$conversation = Conversation::between($user1, $user2);

// Check if a user is part of a conversation
$conversation->> 'Hello!',
]);

// Mark messages as read
$conversation->markAsReadBy(auth()->user());

// Get unread message count
$unreadCount = $conversation->unreadMessagesFor(auth()->user())->count();

// In app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    // Clean up messages older than 90 days, daily at 2 AM
    $schedule->command('live-chat:cleanup --days=90 --force')
        ->dailyAt('02:00');
}

namespace App\Models;

use muba00\LaravelLiveChat\Models\Conversation as BaseConversation;

class Conversation extends BaseConversation
{
    // Add your custom methods and properties
}

// In config/live-chat.php
'routes' => [
    'enabled' => false,
],
bash
php artisan live-chat:install
bash
php artisan reverb:start