labomatik / chat
Laravel 的聊天包
资助包维护!
www.paypal.me/tinashemusonza
Requires
- php: >=7.3
- laravel/framework: ^6.0|^7.0|^8.0|^9.0|^10.0
- laravel/legacy-factories: ^1.0
- spatie/laravel-fractal: ^6.0
- spatie/laravel-medialibrary: ^10.0.0
Requires (Dev)
- mockery/mockery: ^1.0.0
- orchestra/testbench: ^4.0|^6.0
- phpunit/phpunit: ^8.0|^9.0
- dev-master
- v6.0.0
- v5.0.0
- v4.8.6
- v4.8.5
- 4.8.4
- 4.8.3
- v4.8.2
- v4.8.1
- v4.8
- 4.7
- v4.6.1
- v4.6.0
- v4.5.0
- v4.4.0
- v4.3.0
- v4.2.0
- v4.1.1
- v4.1.0
- 4.1.0-beta
- 4.0.5-beta
- 4.0.4-beta
- 4.0.3
- 4.0.1
- 4.0.0
- v4.0.0-rc6
- v4.0.0-rc4
- v4.0.0-rc3
- v4.0.0-rc1
- 4.0.0-beta.1
- v3.7.0
- v3.6.0
- v3.5.0
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.7
- v3.2.6
- v3.2.5
- v3.2.4
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.2
- v3.1.1
- v3.1.0
- 3.0.x-dev
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.0
- v0.1
This package is auto-updated.
Last update: 2024-09-23 13:15:29 UTC
README
为你的多个模型创建聊天应用
目录
点击展开
简介
此包允许您将聊天系统添加到 Laravel 应用程序中,由 musonza/chat 分支而来
安装
在命令行中运行
composer require labomatik/chat
发布资产
php artisan vendor:publish
这将发布数据库迁移和一个配置文件 chat.php
在 Laravel 配置文件夹中。
配置
查看 chat.php
进行配置
运行迁移
php artisan migrate
使用方法
您可以将模型混合作为参与者。例如,您可以有 Parents
、Students
和 Professors
模型进行通信
添加参与模型的能力
将 Musonza\Chat\Traits\Messageable
特性添加到任何您希望参与会话的模型中。例如,假设我们想我们的 Bot
模型与其他模型聊天
use Illuminate\Database\Eloquent\Model; use Musonza\Chat\Traits\Messageable; class Bot extends Model { use Messageable; }
获取参与者详情
由于我们允许具有不同结构的模型进行聊天,我们可能想要以一种统一的方式表示参与者详情。
您可以按以下方式获取详情
$participantModel->getParticipantDetails();
假设您有一个列 name
为您的模型,它返回一个默认数组 ['name' => 'column_value']
。然而,您可以通过向模型添加 Eloquent 访问器来自定义此功能,返回一个包含您需要的所有内容的数组,如下所示
public function getParticipantDetailsAttribute() { return [ 'name' => $this->someValue, 'foo' => 'bar', ]; }
创建会话
您可以通过传递模型数组作为参与者来开始会话
$participants = [$model1, $model2,..., $modelN]; $conversation = Chat::createConversation($participants);
创建类型为私人/公共的会话
您可能希望将会话分类为私人或公共
$participants = [$model1, $model2,..., $modelN]; // Create a private conversation $conversation = Chat::createConversation($participants)->makePrivate(); // Create a public conversation $conversation = Chat::createConversation($participants)->makePrivate(false); // Create a direct message // Make direct conversation after creation $conversation = Chat::createConversation($participants)->makeDirect(); // Specify intent for direct conversation before creation $conversation = Chat::makeDirect()->createConversation($participants);
注意:您无法向直接会话添加更多参与者。此外,您无法从直接会话中删除参与者。
通过 id 获取会话
$conversation = Chat::conversations()->getById($id);
更新会话详情
$data = ['title' => 'PHP Channel', 'description' => 'PHP Channel Description']; $conversation->update(['data' => $data]);
发送文本消息
$message = Chat::message('Hello') ->from($model) ->to($conversation) ->send();
发送自定义类型消息
默认消息类型是 text
。如果您想指定自定义类型,可以调用以下 type()
函数
$message = Chat::message('http://example.com/img') ->type('image') ->from($model) ->to($conversation) ->send();
添加更多消息详情
有时您可能想要添加有关消息的详细信息。例如,当消息类型为附件时,您可能想添加有关附件的文件名和文件 url 的详细信息,您可以调用 data()
函数并将您的数据作为数组传递。
$message = Chat::message('Attachment 1') ->type('attachment') ->data(['file_name' => 'post_image.jpg', 'file_url' => 'http://example.com/post_img.jpg']) ->from($model) ->to($conversation) ->send();
通过 id 获取消息
$message = Chat::messages()->getById($id);
获取消息发送者
$sendModel = $message->sender;
标记消息为已读
Chat::message($message)->setParticipant($participantModel)->markRead();
标记/标记消息
Chat::message($message)->setParticipant($participantModel)->toggleFlag(); Chat::message($message)->setParticipant($participantModel)->flagged(); // true
标记整个会话为已读
Chat::conversation($conversation)->setParticipant($participantModel)->readAll();
未读消息计数
$unreadCount = Chat::messages()->setParticipant($participantModel)->unreadCount();
每个会话的未读消息计数
Chat::conversation($conversation)->setParticipant($participantModel)->unreadCount();
删除消息
Chat::message($message)->setParticipant($participantModel)->delete();
清理已删除消息
当所有参与者都已删除 $message
或 $conversation
时,要清理什么?
监听 \Musonza\Chat\Eventing\AllParticipantsDeletedMessage
和
\Musonza\Chat\Eventing\AllParticipantsClearedConversation
清除会话
Chat::conversation($conversation)->setParticipant($participantModel)->clear();
获取参与者的会话
Chat::conversations()->setPaginationParams(['sorting' => 'desc']) ->setParticipant($participantModel) ->limit(1) ->page(1) ->get();
获取两个参与者之间的会话
$conversation = Chat::conversations()->between($participantModel1, $participantModel2);
获取参与者之间的公共会话
$conversations = Chat::conversations()->common($participants);
$participants
是参与者模型数组
从会话中移除参与者
/* removing one user */ Chat::conversation($conversation)->removeParticipants([$participantModel]);
/* removing multiple participants */ Chat::conversation($conversation)->removeParticipants([$participantModel, $participantModel2,...,$participantModelN]);
向会话中添加参与者
/* add one user */ Chat::conversation($conversation)->addParticipants([$participantModel]);
/* add multiple participants */ Chat::conversation($conversation)->addParticipants([$participantModel, $participantModel2]);
获取会话中的消息
Chat::conversation($conversation)->setParticipant($participantModel)->getMessages()
按类型获取用户会话
// private conversations $conversations = Chat::conversations()->setParticipant($participantModel)->isPrivate()->get(); // public conversations $conversations = Chat::conversations()->setParticipant($participantModel)->isPrivate(false)->get(); // direct conversations / messages $conversations = Chat::conversations()->setParticipant($participantModel)->isDirect()->get(); // all conversations $conversations = Chat::conversations()->setParticipant($participantModel)->get();
获取最近的消息
$messages = Chat::conversations()->setParticipant($participantModel)->limit(25)->page(1)->get();
获取包含已存档对话的最近消息
$messages = Chat::conversations(true)->setParticipant($participantModel)->limit(25)->page(1)->get();
分页
有几种方法可以实现分页。您可以使用相应的函数指定 limit
和 page
,如下所示,或者如下所示
$paginated = Chat::conversations()->setParticipant($participant)
->setPaginationParams([
'page' => 3,
'perPage' => 10,
'sorting' => "desc",
'columns' => [
'*'
],
'pageName' => 'test'
])
->get();
您不必指定所有参数。如果您省略了参数,将使用默认值。$paginated
上将返回 Illuminate\Pagination\LengthAwarePaginator
。要获取 conversations
,只需调用 $paginated->items()
获取会话中的参与者
$participants = $conversation->getParticipants();
获取模型在会话中的参与条目
Chat::conversation($conversation)->getParticipation($model);
更新参与设置
为参与者设置对话设置(例如:mute_mentions, mute_conversation)
$settings = ['mute_mentions' => true]; Chat::conversation($conversation) ->getParticipation($this->alpha) ->update(['settings' => $settings]);
数据转换器
需要更多控制从包路由返回的数据?您可以指定自己的模型转换器,并利用 Fractal。
您需要做的就是指定配置文件 chat.php
中转换器的位置,如下所示
/** * Model Transformers */ 'transformers' => [ 'conversation' => \MyApp\Transformers\ConversationTransformer::class, 'message' => \MyApp\Transformers\MessageTransformer::class, 'participant' => \MyApp\Transformers\ParticipantTransformer::class, ]
注意: 这仅适用于包路由的响应。
许可
Chat 是开源软件,使用 MIT 许可证 许可。