Skip to content

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:

php
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:

php
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.

php
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:

php
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.

php
// Example usage
$department = Department::find(1);
$allMessages = $department->messages;

withFilters($filters) ​

Lấy tin nhắn theo các bộ lọc chỉ định.

php
// 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.

php
$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.

php
$markedCount = $department->markAsRead();

addMessage(array $data): Message ​

Thêm tin nhắn mới vào model.

php
$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.

php
$reply = $department->replyToMessage($existingMessage, [
    'body' => 'This is a reply to the previous message'
]);

removeMessage($messageId, $type = 'messages'): bool ​

Xoá tin nhắn theo ID.

php
$deleted = $department->removeMessage(5);

pinMessage(Message $message): bool ​

Ghim tin nhắn để làm nổi bật.

php
$pinned = $department->pinMessage($message);

unpinMessage(Message $message): bool ​

Bỏ ghim tin nhắn đã ghim trước đó.

php
$unpinned = $department->unpinMessage($message);

getPinnedMessages(): Collection ​

Lấy mọi tin nhắn đã ghim của model.

php
$pinnedMessages = $department->getPinnedMessages();

getMessagesByType(string $type): Collection ​

Lấy tin nhắn thuộc một loại cụ thể.

php
$notes = $department->getMessagesByType('note');

getInternalMessages(): Collection ​

Lấy mọi tin nhắn nội bộ.

php
$internalMessages = $department->getInternalMessages();

getMessagesByDateRange(Carbon $startDate, Carbon $endDate): Collection ​

Lấy tin nhắn trong một khoảng ngày chỉ định.

php
$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ể.

php
$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.

php
$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 đè).

php
$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.

php
$allAttachments = $department->attachments;

addAttachments(array $files, array $additionalData = []): Collection ​

Thêm nhiều tệp đính kèm vào model.

php
$attachments = $department->addAttachments([
    'documents/policy.pdf',
    'images/organization.png'
], [
    'description' => 'Department documents'
]);

removeAttachment($attachmentId): bool ​

Xoá tệp đính kèm theo ID.

php
$removed = $department->removeAttachment(3);

getAttachmentsByType(string $mimeType): Collection ​

Lấy tệp đính kèm theo một kiểu MIME cụ thể.

php
$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.

php
$recentAttachments = $department->getAttachmentsByDateRange(
    Carbon::now()->subDays(7),
    Carbon::now()
);

getImageAttachments(): Collection ​

Lấy mọi tệp đính kèm là ảnh.

php
$images = $department->getImageAttachments();

getDocumentAttachments(): Collection ​

Lấy mọi tệp đính kèm là tài liệu (không phải ảnh).

php
$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.

php
$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.

php
$followers = $department->followers;

addFollower(Partner $partner): Follower ​

Thêm một đối tác làm người theo dõi model.

php
$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.

php
$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.

php
$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:

php
$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ọcMô tảVí dụ
typeMảng các loại tin nhắn['note', 'comment']
is_internalGiá trị đúng/sai cho tin nhắn nội bộtrue hoặc false
date_fromNgày bắt đầu lọc'2025-01-01'
date_toNgày kết thúc lọc'2025-03-13'
causer_idID người tạo tin nhắn5
causer_typeKiểu của người tạo tin nhắn'Webkul\\Security\\Models\\User'
assigned_toID người dùng được giao3
activity_type_idID loại hoạt động2
company_idID công ty1
searchTừ khoá tìm trong tiêu đề/nội dung/tóm tắt'important'

Nội dung dựa trên AureusERP User Guide của Webkul (giấy phép MIT), Việt hoá và biên tập bởi Softway.