This package gives your Laravel app a lightweight, backend-only chat system. It supports multiple users per conversation, per-message read tracking, pivot-level read pointers, and optional model attachment via polymorphic relations.
- Multiple participants per conversation
- Per-message read receipts (
MessageReadmodel) - Fast unread queries via denormalized
last_read_message_idon the pivot - Mute and archive conversations per user
- Attach conversations to any Eloquent model (e.g. a
Mission,Order, orProject) - Widget messages for rich content (e.g. custom Livewire/Vue components)
- Markdown helper with inline-only parsing, autolinks, and safe external links
- PHP ^8.1
- Laravel 11, 12, or 13
composer require elegantly/laravel-conversationsPublish and run migrations:
php artisan vendor:publish --tag="conversations-migrations"
php artisan migratePublish the config (optional):
php artisan vendor:publish --tag="conversations-config"Add the ParticipateToConversations trait to your User model:
use Elegantly\Conversation\Concerns\ParticipateToConversations;
class User extends Authenticatable
{
use ParticipateToConversations;
}If you want a conversation tied to, say, a Mission or Project, use HasConversation:
use Elegantly\Conversation\Concerns\HasConversation;
class Mission extends Model
{
use HasConversation;
}use Elegantly\Conversation\Conversation;
$conversation = new Conversation();
$conversation->save();
// Attach participants
$conversation->users()->sync([$user->id, $user2->id]);
// Optional: attach to a parent model
$conversation->conversationable()->associate($mission);
$conversation->save();
// Optional: set an owner
$conversation->owner()->associate($admin);
$conversation->save();use Elegantly\Conversation\Message;
$message = new Message([
'content' => 'Hey team, the deployment is live!',
]);
$message->user()->associate(auth()->user());
$conversation->send($message);When you call send(), the package will:
- Save the message
- Update the conversation’s
latest_message_idandmessaged_at - Automatically mark the message as read for the sender
$message->markAsReadBy($user);
// Or force-update the read timestamp
$message->markAsReadBy($user, force: true);$message->markAsUnreadBy($user);$message->isReadBy($user);
$message->isNotReadBy($user);
$message->isReadByAnyone();
$message->isReadByAll([$user1, $user2]);
// Read timestamp
$message->getReadByAt($user);$conversation
->getConversationUser($user)
?->markAsRead($message);This updates the conversation_user.last_read_message_id column, which makes unread-conversation queries extremely fast.
use Elegantly\Conversation\Conversation;
// Conversations with unread messages for a user
Conversation::query()->unreadBy($user)->get();
// Conversations fully read by a user
Conversation::query()->readBy($user)->get();// Fast unread queries using the pivot column
$user->denormalizedUnreadConversations()->get();
$user->denormalizedReadConversations()->get();// Messages inside a conversation
$conversation->messages()->unreadBy($user)->get();
$conversation->messages()->readBy($user)->get();
// Sent by a specific user
$conversation->messages()->byUser($user)->get();
// Not sent by a specific user
$conversation->messages()->notByUser($user)->get();Each participant can mute or archive a conversation via the pivot:
// Mute
$conversation->users()->updateExistingPivot($user->id, ['muted_at' => now()]);
// Unmute
$conversation->users()->updateExistingPivot($user->id, ['muted_at' => null]);
// Archive
$conversation->users()->updateExistingPivot($user->id, ['archived_at' => now()]);
// Unarchive
$conversation->users()->updateExistingPivot($user->id, ['archived_at' => null]);Convenience accessors on the user:
$user->conversationsNotMuted()->get();
$user->conversationsMuted()->get();
$user->conversationsNotArchived()->get();
$user->conversationsArchived()->get();Sometimes a message is not plain text but a UI component. You can store a widget payload:
$message = new Message();
$message->user()->associate($user);
$message->setWidget('invoice-widget', [
'invoice_id' => 123,
'total' => 499.00,
]);
$conversation->send($message);Helpers on the message:
$message->hasWidget(); // true
$message->getWidgetComponent(); // 'invoice-widget'
$message->getWidgetProps(); // ['invoice_id' => 123, 'total' => 499.0, 'message' => $message]Messages can be rendered as safe inline Markdown:
// On an existing message
$message->toMarkdown();
// Or manually
Message::markdown($rawString);This uses league/commonmark with inline-only, autolinks, and safe external links.
return [
'model_user' => User::class,
'model_message' => Message::class,
'model_conversation' => Conversation::class,
'model_conversation_user' => ConversationUser::class,
'model_read' => MessageRead::class,
// When a User is deleted, also delete his messages
'cascade_user_delete_to_messages' => false,
// When a Conversation is deleted, also delete its messages
'cascade_conversation_delete_to_messages' => false,
// When the parent model is deleted, also delete the conversation
'cascade_conversationable_delete_to_conversation' => false,
'markdown' => [
'environment' => [
'allow_unsafe_links' => false,
'html_input' => 'strip',
'external_link' => [
'internal_hosts' => env('APP_URL', '') ?: null,
'open_in_new_window' => true,
'html_class' => '',
'nofollow' => 'external',
'noopener' => 'external',
'noreferrer' => 'external',
],
],
],
];You can extend any model and override it in the config. For example, if you need extra casts or methods on Message:
namespace App\Models;
use Elegantly\Conversation\Message as BaseMessage;
class Message extends BaseMessage
{
protected static function booted(): void
{
parent::booted();
// your logic
}
}Then update config/conversations.php:
'model_message' => App\Models\Message::class,composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.