benwilkins / yak
一个Laravel消息系统
README
一个为Laravel框架构建的强大消息系统。创建多用户之间的对话或一对一对话。
适用于Laravel 5.4及以上版本。
安装
只需几个简单的步骤即可开始发送消息
1. 使用Composer添加到Laravel
composer require benwilkins/yak
2. 注册服务提供者和外观
在config/app.php
中添加以下内容
'providers' => [ // ... Benwilkins\Yak\YakServiceProvider::class, // ... ], 'aliases' => [ // ... 'Yak' => Benwilkins\Yak\Facades\Yak::class, // ... ],
3. 发布配置
php artisan vendor:publish
4. 运行迁移
php artisan migrate
5. 将特性添加到您的用户模型中
<?php namespace App; use Benwilkins\Yak\Traits\Messageable; class User extends Authenticatable { use Notifiable, Messageable; // ...
使用方法
对话
一个对话可以存在于两个或多个参与者之间。参与者可以通过对话模型中包含的一些方法轻松添加和删除。
开始或查找对话
Yak包括一个方法,可以轻松查找或创建参与者之间的对话。
// One easy method $conversation = Yak::startOrFindConversation([$userId1, $userId2, ...]); // Find a conversation manually $conversation = Yak::findConversation([$userId1, $userId2, ...]);
添加/删除参与者
您可以轻松地将参与者添加或从对话中删除。如果对话中只有两个参与者,尝试删除其中一个将抛出异常。
// Adding many participants $conversation->addParticipants([$userId1, $userId2, ...]); // Adding one participant $conversation->addParticipants($userId); // Removing many participants $conversation->removeParticipants([$userId1, $userId2, ...]); // Removing one participant $conversation->removeParticipants($userId);
消息
发送消息
Yak还包括一种方法可以向一个或多个用户发送消息。这个方法将找到或创建一个对话,并自动将消息添加到其中,只需一个简单的步骤。
$conversation = Yak::sendMessageToParticipants([$userId1, $userId2, ...]);
对话状态
对话状态将确定给定用户是否对对话有未读消息。对话模型将包含一个state_for_current_user
属性。
参与者(通过Messageable特性使用用户)
通过将Messageable
特性添加到您的用户模型中,您可以获取给定用户的全部对话和消息。
// Get all conversations for the user $conversations = $user->conversations; // Get all messages sent by the user $messages = $user->messages; // Get a count of unread messages $unreadCount = $user->unreadMessageCount(); // Get all conversations containing unread messages $unreadConversations = $user->unreadConversations();
您还可以获取一个用户的“对话列表”,该列表将返回所有未读对话在列表顶部,然后是下一个八个已读对话。您可以覆盖要附加到列表中的已读对话数量。
$conversations = $user->conversationList($readCount = 8);
事件
ConversationStarted
当使用Yak外观启动对话时,将触发Benwilkins\Yak\Events\ConversationStarted
事件。
MessageSent
当创建新消息时,将触发Benwilkins\Yak\Events\MessageSent
事件。
YakConversationParticipantAdded
当通过对话模型的addParticipants
方法添加参与者时,将触发Benwilkins\Yak\Events\ConversationParticipantAdded
事件。
YakConversationParticipantRemoved
当通过对话模型的removeParticipants
方法删除参与者时,将触发Benwilkins\Yak\Events\ConversationParticipantRemoved
事件。
合同
可以扩展包中的所有模型,甚至扩展Yak外观。要这样做,只需创建并注册一个新的服务提供程序,将模型合同绑定到您想要使用的模型。
注意:强烈建议仅扩展当前模型。其中包含对应用程序重要的启动方法和模型事件。
示例
<?php namespace App\Providers; // App\MyConversation will extend Benwilkins\Yak\Models\Conversation // and implement Benwilkins\Yak\Contracts\Models\Conversation use App\MyConversation; use Benwilkins\Yak\Contracts\Models\Conversation as ConversationContract; use Illuminate\Support\ServiceProvider; class YakServiceProvider extends ServiceProvider { public function register() { $this->app->bind( ConversationContract::class, MyConversation::class ); } }
并在config/app.php
'providers' => [ ... Benwilkins\Yak\YakServiceProvider::class, App\Providers\YakServiceProvider::class, // Make sure this one comes after the one before it. ... ],