inani/

messager

一个用于处理消息的简单包

1.0.2 2017-03-06 10:26 UTC

This package is auto-updated.

Last update: 2024-09-08 21:29:10 UTC


README

(不再维护)

Laravel Messager

以简单方式处理用户之间消息的便捷方式

目录

  1. 安装
  2. 设置模型
  3. 创建与发送消息
    1. 创建消息
    2. 发送消息
    3. 回复消息
    4. 草拟消息
  4. 处理消息
    1. 获取用户间消息
    2. 阅读消息
    3. 未读消息
    4. 草稿消息
  5. 标签
    1. 创建和编辑标签
    2. 将标签分配给消息
    3. 更改和获取消息的标签
    4. 从消息中移除标签

安装

首先,通过Composer安装此包。

composer require inani/messager

然后在 config/app.php 中包含服务提供者。

'providers' => [
    ...
    Inani\Messager\MessagerServiceProvider::class,
    ...
];

发布配置和迁移文件

php artisan vendor:publish

设置模型

要设置模型,只需添加(并导入)MessageAccessible 特性。

use Inani\Messager\Helpers\MessageAccessible;
use Inani\Messager\Helpers\TagsCreator;
class User extends Model
{
    use MessageAccessible, TagsCreator;
    ...
}

创建与发送消息

创建消息

$receiver = User::find(1); 

// Message Data
$messageData = [
	'content' => 'Hello all this is just a test', // the content of the message
	'to_id' => $receiver->getKey(), // Who should receive the message
];

list($message, $user) = App\User::createFromRequest($messageData);

发送消息

$sender = User::find(2);

$sent = $sender->writes($message)
                 ->to($user)
                 ->send();
		 
// send to multiple users the same message
// can execpt a user|array of users| array of ids| or array of users and ids
$sent = $sender->writes($message)
                 ->to($user)
		 ->cc([$user1, $user2])
		 ->cc([$user3->id, $user4->id])
                 ->send();

回复消息

$sender = User::find(2);

$sent = $user->writes($newMessage)
                 ->to($sender)
		 ->responds($message)
                 ->send();

草拟消息

$sender = User::find(2);

$draft = $sender->writes($message)
                  ->to($user)
                  ->draft()
                  ->keep();

处理消息

一旦你有了消息,你需要对它们进行一些操作。

获取用户间消息

// Users
$userA = App\User::find(1);
$userB = App\User::find(2);

// Get seen messages sent from UserB to UserA
$messages = $userA->received()->from($userB)->seen()->get();

// OR you can pass an array of IDs 
$messages = $userA->received()->from([2, 3, 4, 5])->seen()->get();

阅读消息

// Set the selected message(or id of messages as read)
$count = $userB->received()->select($message)->readThem();

未读消息

// Get unread messages from UserB to User A
$messages = $userA->received()->from($userB)->unSeen()->get();

// Marking them as read
$messages = $userA->received()->from($userB)->unSeen()->readThem();

// check out if a conversation has new messages
$bool = $userA->received()->conversation($message)->hasNewMessages();

// Get the number of conversations that have new messages in it
$number = $userA->received()->unSeenConversations();

已发送消息

// Get unread messages from UserA to UserB
$messages = $userA->sent()->to($userB)->get();

// OR you can pass an array of IDs
$messages = $userA->received()->to([2, 3, 4, 5)->get();

草稿消息

// Get the draft messages for UserA
$messages = $userA->sent()->inDraft()->get().

标签

你可以标记(或将你的消息结构化到不同的类别中)。

创建和编辑标签

每个用户可以创建任意数量的标签。

// create a new tag, $data can be (Tag instance, array, Request)
$tag = $userA->addNewTag($data);

// Modify the attributes of a tag
$user->tag($tag)->name("social")->color("#ffff")->apply();

将标签分配给消息

一旦你有了消息和标签

// you'll need the instance of user(to check if sender or receiver)
// $user and $tag can be ids or instance of User, Tag classes
$bool = $message->concerns($user)->putTag($tag);

更改和获取消息的标签

// to change the tag just use the same method
$bool = $message->concerns($user)->putTag($tag);

// to get the tag of the message, null if not tagged
$tagOrNull = $message->concerns($user)->getTag();
// 

从消息中移除标签

// To remove the tag from the message
$bool = $message->concerns($user)->removeTag();