ab192130 / messages
此包已被弃用,不再维护。没有推荐替代包。
适用于Laravel 5.4及以上的消息包
v3.4.2
2019-08-14 19:19 UTC
Requires
- php: >=7.0
- laravel/framework: ~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0
Requires (Dev)
- mockery/mockery: ^1.0.0
- orchestra/database: ~3.3.0|~3.4.2|^3.5.0|~3.7.0
- orchestra/testbench: ~3.3.0|~3.4.2|^3.5.0|~3.7.0
- phpunit/phpunit: ^5.7|6.2|^7.0
README
聊天
简介
此包允许您将聊天系统添加到您的Laravel ^5.4应用中
安装
在命令行中运行
composer require musonza/chat
将服务提供者添加到您的 config\app.php
文件中的 providers 数组
Musonza\Chat\ChatServiceProvider::class
将外观添加到您的别名中
'Chat' => Musonza\Chat\Facades\ChatFacade::class to your `config\app.php`
类绑定到 ioC 上的 chat
$chat = App::make('chat');
发布资产
php artisan vendor:publish
这将发布数据库迁移和配置文件 musonza_chat.php
到 Laravel 配置文件夹。
配置
return [ 'user_model' => 'App\User', /** * If not set, the package will use getKeyName() on the user_model specified above */ 'user_model_primary_key' => null, /* * This will allow you to broadcast an event when a message is sent * Example: * Channel: mc-chat-conversation.2, * Event: Musonza\Chat\Eventing\MessageWasSent */ 'broadcasts' => false, /** * The event to fire when a message is sent * See Musonza\Chat\Eventing\MessageWasSent if you want to customize. */ 'sent_message_event' => 'Musonza\Chat\Eventing\MessageWasSent', /** * Automatically convert conversations with more than two users to public */ 'make_three_or_more_users_public' => true, ];
运行迁移
php artisan migrate
使用
默认情况下,该包假设您在 App 命名空间中有一个用户模型。
但是,您可以更新在 config
文件夹中发布的 musonza_chat.php
中的用户模型。
创建对话
$participants = [$userId, $userId2,...]; $conversation = Chat::createConversation($participants);
创建私有/公开类型的对话
$participants = [$userId, $userId2,...]; // Create a private conversation $conversation = Chat::createConversation($participants)->makePrivate(); // Create a public conversation $conversation = Chat::createConversation($participants)->makePrivate(false);
通过ID获取对话
$conversation = Chat::conversations()->getById($id);
更新对话详情
$data = ['title' => 'PHP Channel', 'description' => 'PHP Channel Description']; $conversation->update(['data' => $data]);
发送文本消息
$message = Chat::message('Hello') ->from($user) ->to($conversation) ->send();
发送自定义类型的消息
默认消息类型为 text
。如果您想指定自定义类型,可以调用下面的 type()
函数
$message = Chat::message('http://example.com/img') ->type('image') ->from($user) ->to($conversation) ->send();
通过ID获取消息
$message = Chat::messages()->getById($id);
将消息标记为已读
Chat::message($message)->setUser($user)->markRead();
标记/标记消息
Chat::message($message)->setUser($user)->toggleFlag(); Chat::message($message)->setUser($user)->flagged(); // true
将整个对话标记为已读
Chat::conversation($conversation)->setUser($user)->readAll();
未读消息数量
$unreadCount = Chat::messages()->setUser($user)->unreadCount();
每场对话未读消息数
Chat::conversation($conversation)->setUser($user)->unreadCount();
删除一条消息
Chat::message($message)->setUser($user)->delete();
清除对话
Chat::conversation($conversation)->setUser($user)->clear();
获取两个用户之间的对话
$conversation = Chat::conversations()->between($user1, $user2);
获取用户之间的公共对话
$conversations = Chat::conversations()->common($users);
$users
可以是一个用户ID数组,例如 [1,4,6]
,或者是一个用户集合 (\Illuminate\Database\Eloquent\Collection)
从对话中移除用户
/* removing one user */ Chat::conversation($conversation)->removeParticipants($user);
/* removing multiple users */ Chat::conversation($conversation)->removeParticipants([$user1, $user2, $user3,...,$userN]);
将用户添加到对话中
/* add one user */ Chat::conversation($conversation)->addParticipants($user);
/* add multiple users */ Chat::conversation($conversation)->addParticipants([$user3, $user4]);
注意:默认情况下,第三个用户会将原本为私密的对话分类为非私密。有关如何更改此设置,请参阅配置文件。
获取对话中的消息
Chat::conversation($conversation)->setUser($user)->getMessages()
按类型获取用户对话
// private conversations $conversations = Chat::conversations()->setUser($user)->isPrivate()->get(); // public conversations $conversations = Chat::conversations()->setUser($user)->isPrivate(false)->get(); // all conversations $conversations = Chat::conversations()->setUser($user)->get();
获取最近消息
$messages = Chat::conversations()->setUser($user)->limit(25)->page(1)->get();
示例
[
"id" => 1
"private" => "1"
"data" => []
"created_at" => "2018-06-02 21:35:52"
"updated_at" => "2018-06-02 21:35:52"
"last_message" => array:13 [
"id" => 2
"message_id" => "2"
"conversation_id" => "1"
"user_id" => "1"
"is_seen" => "1"
"is_sender" => "1"
"flagged" => false
"created_at" => "2018-06-02 21:35:52"
"updated_at" => "2018-06-02 21:35:52"
"deleted_at" => null
"body" => "Hello 2"
"type" => "text"
"sender" => array:7 [
"id" => 1
"name" => "Jalyn Ernser"
"email" => "colt.howell@example.com"
]
]
]
分页
您可以通过几种方式实现分页。您可以使用相应的函数指定 limit
和 page
,如下所示,或者如下所示
$paginated = Chat::conversations()->setUser($user)
->setPaginationParams([
'page' => 3,
'perPage' => 10,
'sorting' => "desc",
'columns' => [
'*'
],
'pageName' => 'test'
])
->get();
您不需要指定所有参数。如果您省略了参数,将使用默认值。上面的 $paginated
将返回 Illuminate\Pagination\LengthAwarePaginator
。要获取 conversations
,只需调用 $paginated->items()
获取对话中的用户
$users = $conversation->users;
许可证
聊天是开源软件,许可协议为 MIT许可证