Tổng quan
Trait HasChatter cung cấp một hệ thống trao đổi linh hoạt cho các model Laravel trong Softway ERP. Khi áp dụng vào một model, nó bật tính năng chatter, gồm tin nhắn, hoạt động, tệp đính kèm và quản lý người theo dõi.
Ngoài ra, trait HasLogActivity hoạt động cùng HasChatter để tự động ghi lại các thay đổi của model thành tin nhắn hoạt động. Nó theo dõi các sự kiện của model (tạo, cập nhật, xoá) và ghi chúng thành các mục hoạt động có cấu trúc, hiển thị được trong luồng chatter.
Dùng HasChatter
Để bật tính năng chatter cho model, dùng trait HasChatter:
use Webkul\Chatter\Traits\HasChatter;
class YourModel extends Model
{
use HasChatter;
// Rest of your model definition
}Dùng HasChatter cùng HasLogActivity
Để ghi các thay đổi của model thành tin nhắn hoạt động, thêm cả hai trait HasChatter và HasLogActivity:
use Webkul\Chatter\Traits\HasChatter;
use Webkul\Chatter\Traits\HasLogActivity;
class YourModel extends Model
{
use HasChatter, HasLogActivity;
// Rest of your model definition
}Các thuộc tính được ghi log
Mảng $logAttributes dưới đây định nghĩa các thuộc tính sẽ được ghi lại khi model thay đổi. Mảng này gồm cả thuộc tính trực tiếp của model lẫn thuộc tính của model liên quan.
protected array $logAttributes = [
'medium.name' => 'Medium',
'utmSource.name' => 'UTM Source',
'partner.name' => 'Customer',
//
];Tuỳ chỉnh tiêu đề quan hệ
Nếu muốn đổi tiêu đề của một thuộc tính quan hệ trong log, bạn cập nhật khoá tương ứng trong mảng $logAttributes.
Ví dụ, để đổi tiêu đề log của thuộc tính partner.name từ "Customer" thành "Client", bạn có thể viết:
protected array $logAttributes = [
'partner.name' => 'Client',
];Nhờ vậy, khi ghi lại thay đổi, tiêu đề mới sẽ được dùng thay cho tiêu đề mặc định.
Các phương thức có sẵn
Quản lý tin nhắn
messages(): MorphMany
Lấy mọi tin nhắn không phải hoạt động của model, sắp xếp theo ngày tạo giảm dần.
// Example usage
$department = Department::find(1);
$allMessages = $department->messages;withFilters($filters)
Lấy tin nhắn theo các bộ lọc chỉ định.
// Example usage
$filters = [
'type' => ['note', 'comment'],
'is_internal' => true,
'date_from' => '2025-01-01',
'date_to' => '2025-03-13',
'search' => 'important'
];
$filteredMessages = $department->withFilters($filters);unRead()
Lấy mọi tin nhắn chưa đọc của model.
$unreadMessages = $department->unRead();markAsRead(): int
Đánh dấu mọi tin nhắn chưa đọc là đã đọc, trả về số tin nhắn đã cập nhật.
$markedCount = $department->markAsRead();addMessage(array $data): Message
Thêm tin nhắn mới vào model.
$message = $department->addMessage([
'subject' => 'New policy update',
'body' => 'We are updating the department policies...',
'type' => 'note'
]);replyToMessage(Message $parentMessage, array $data): Message
Thêm trả lời cho một tin nhắn có sẵn.
$reply = $department->replyToMessage($existingMessage, [
'body' => 'This is a reply to the previous message'
]);removeMessage($messageId, $type = 'messages'): bool
Xoá tin nhắn theo ID.
$deleted = $department->removeMessage(5);pinMessage(Message $message): bool
Ghim tin nhắn để làm nổi bật.
$pinned = $department->pinMessage($message);unpinMessage(Message $message): bool
Bỏ ghim tin nhắn đã ghim trước đó.
$unpinned = $department->unpinMessage($message);getPinnedMessages(): Collection
Lấy mọi tin nhắn đã ghim của model.
$pinnedMessages = $department->getPinnedMessages();getMessagesByType(string $type): Collection
Lấy tin nhắn thuộc một loại cụ thể.
$notes = $department->getMessagesByType('note');getInternalMessages(): Collection
Lấy mọi tin nhắn nội bộ.
$internalMessages = $department->getInternalMessages();getMessagesByDateRange(Carbon $startDate, Carbon $endDate): Collection
Lấy tin nhắn trong một khoảng ngày chỉ định.
$messages = $department->getMessagesByDateRange(
Carbon::parse('2025-01-01'),
Carbon::parse('2025-03-13')
);getMessagesByActivityType(int $activityTypeId): Collection
Lấy tin nhắn gắn với một loại hoạt động cụ thể.
$activityMessages = $department->getMessagesByActivityType(3);Quản lý hoạt động
activities(): MorphMany
Lấy mọi tin nhắn hoạt động của model.
$activities = $department->activities;activityPlans(): mixed
Trả về tập hợp các kế hoạch hoạt động (mặc định rỗng, có thể ghi đè).
$plans = $department->activityPlans();Quản lý tệp đính kèm
attachments(): MorphMany
Lấy mọi tệp đính kèm của model.
$allAttachments = $department->attachments;addAttachments(array $files, array $additionalData = []): Collection
Thêm nhiều tệp đính kèm vào model.
$attachments = $department->addAttachments([
'documents/policy.pdf',
'images/organization.png'
], [
'description' => 'Department documents'
]);removeAttachment($attachmentId): bool
Xoá tệp đính kèm theo ID.
$removed = $department->removeAttachment(3);getAttachmentsByType(string $mimeType): Collection
Lấy tệp đính kèm theo một kiểu MIME cụ thể.
$pdfAttachments = $department->getAttachmentsByType('application/pdf');getAttachmentsByDateRange(Carbon $startDate, Carbon $endDate): Collection
Lấy tệp đính kèm trong một khoảng ngày chỉ định.
$recentAttachments = $department->getAttachmentsByDateRange(
Carbon::now()->subDays(7),
Carbon::now()
);getImageAttachments(): Collection
Lấy mọi tệp đính kèm là ảnh.
$images = $department->getImageAttachments();getDocumentAttachments(): Collection
Lấy mọi tệp đính kèm là tài liệu (không phải ảnh).
$documents = $department->getDocumentAttachments();attachmentExists($attachmentId): bool
Kiểm tra tệp đính kèm có tồn tại trong bộ lưu trữ hay không.
$exists = $department->attachmentExists(5);Quản lý người theo dõi
followers(): MorphMany
Lấy mọi người theo dõi của model.
$followers = $department->followers;addFollower(Partner $partner): Follower
Thêm một đối tác làm người theo dõi model.
$follower = $department->addFollower($partner);removeFollower(Partner $partner): bool
Gỡ một đối tác khỏi danh sách người theo dõi model.
$removed = $department->removeFollower($partner);isFollowedBy(Partner $partner): bool
Kiểm tra một đối tác có đang theo dõi model hay không.
$isFollowing = $department->isFollowedBy($partner);Dùng HasChatter trong model
Trong model Department, trait HasChatter đã được thêm sẵn, nên bạn có thể dùng trực tiếp mọi phương thức ở trên:
$department = Department::find(1);
// Add a message
$message = $department->addMessage([
'subject' => 'Department Update',
'body' => 'New policy changes are in effect from today',
'type' => 'announcement'
]);
// Add attachments
$department->addAttachments([
'policies/updated_rules.pdf',
'images/department_structure.png'
]);
// Check followers
$isFollowing = $department->isFollowedBy($somePartner);Các tuỳ chọn lọc
Khi dùng phương thức withFilters, bạn có thể áp dụng các bộ lọc sau:
| Bộ lọc | Mô tả | Ví dụ |
|---|---|---|
type | Mảng các loại tin nhắn | ['note', 'comment'] |
is_internal | Giá trị đúng/sai cho tin nhắn nội bộ | true hoặc false |
date_from | Ngày bắt đầu lọc | '2025-01-01' |
date_to | Ngày kết thúc lọc | '2025-03-13' |
causer_id | ID người tạo tin nhắn | 5 |
causer_type | Kiểu của người tạo tin nhắn | 'Webkul\\Security\\Models\\User' |
assigned_to | ID người dùng được giao | 3 |
activity_type_id | ID loại hoạt động | 2 |
company_id | ID công ty | 1 |
search | Từ khoá tìm trong tiêu đề/nội dung/tóm tắt | 'important' |

