承接 ssntpl/laravel-comments 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ssntpl/laravel-comments

Composer 安装命令:

composer require ssntpl/laravel-comments

包简介

Polymorphic comments for Eloquent models: threading, reactions, per-comment files, opt-in edit history, mentions, and lifecycle events.

README 文档

README

This is a simple package to associate comments with your eloquent model in laravel. This package is providing functionality for adding, retrieving, editing, and deleting comments along with its associated files if any on various entities:

Installation

You can install the package via composer:

composer require ssntpl/laravel-comments

Run the migrations with:

php artisan migrate

Optionally, You can publish and run the migrations with:

php artisan vendor:publish --tag="laravel-comments-migrations"
php artisan migrate

Publish the config to customise behaviour (user model, edit history, mention pattern, etc.):

php artisan vendor:publish --tag="laravel-comments-config"

Apps that already own a comments table (and manage the schema themselves) can set comments.auto_load_migrations to false so the package does not register its own migrations.

Usage

Add the HasComments trait to your model.

namespace App\Models;
use Ssntpl\LaravelComments\Traits\HasComments;

class Post extends Model
{
    use HasComments;
}

Add new comment to the model.

$model = Post::find(1);

$comment = $model->createComment([
    
    // type: Optional. It represents the type of comment.
    'type' => 'commentType', 

    // body: This is the body of the comment.
    // (The `text` and `comment` keys are still accepted as backward-compatible aliases.)
    'body' => 'This is the body of the comment', 

    // user_id: Optional. This is a foreign key belonging to that entity who is making the comment. For e.g:Users(so it will be the id of User who is making the comment).
    'user_id' => 1,

    // created_at: The created_at timestamp is automatically managed by Eloquent. Represents the time at which the comment is made. Otherwise one can manually assign a value to created_at when creating a new comment.
    'created_at' => '2025-01-17 05:14:13',  
]);

Accessing the comment model.

$card = Card::find(1);//comments can be added on a card

$card->comments; //return all the comments linked with the card

$card->comments() // returns the Illuminate\Database\Eloquent\Relations\MorphMany relation

$card->comments()->where('user_id',1)->get()  //Accessing all comments of the card made for particular user

$comment = $card->comments()->where('id',3)->first() // One can access the specific comment of card

// to create an attachment on that comment
$comment->createFile([
                    'key' => 'path/filename.jpg',
                    'name' => 'filename.jpg'
                ]);

$comment->file //To access the first or only attachment with that comment on the card

$comment->files //To access all the attachments related to that comment on that card

//One can update particular comment by adding id as one of the params
$card->createComment(['id' => 23, 'user_id' => 2,'body'=> "This is a new comment"])

// The comment body is stored in the `body` column. For backward compatibility,
// `$comment->text` and `$comment->comment` both read from / write to `body`.
$comment->body;    // "This is a new comment"
$comment->text;    // same value (alias)
$comment->comment; // same value (alias)

//Like this one can delete all the comments along with its attachment on the card
$comments = $card->comments()->get();
foreach($comments as $comment) {
    $comment->delete();
}

Threading (replies)

A comment can reply to another via reply_to_comment_id:

$reply = $card->createComment([
    'user_id' => 2,
    'body' => 'I agree',
    'reply_to_comment_id' => $comment->id,
]);

$reply->replyToComment; // the parent comment
$comment->replies;      // replies to this comment
$card->rootComments();  // top-level comments only (excludes replies)

Reactions

One reaction per user per comment (re-reacting changes it):

$comment->react($user, 'thumbsup'); // add or change $user's reaction
$comment->unreact($user);           // remove it
$comment->reactions;                // all reactions on the comment

Edit history (opt-in)

Set comments.changelog to true to snapshot each edit. On every change to body, the previous body is stored in comment_changelogs, so the changelog table holds all prior versions and the model holds the current one — the full history is reconstructable.

$comment->changelogs; // prior versions, newest first

Mentions

The package parses @handles out of the body; resolving them to users and delivering notifications is your app's job (typically in a listener on the events below).

$comment->mentionedHandles(); // ['bob', 'carol.dev']

The pattern is configurable via comments.mentions.pattern.

Events

The package fires framework-agnostic events so your app can send notifications, broadcast, or record activity without the package knowing about any of that:

Event When
CommentCreated a comment is created
CommentUpdated a comment's body is edited (carries previousBody)
CommentDeleted a comment is deleted
ReactionAdded a user reacts (or changes their reaction)
ReactionRemoved a user removes their reaction
Event::listen(\Ssntpl\LaravelComments\Events\CommentCreated::class, function ($event) {
    // $event->comment->mentionedHandles(), notify, broadcast, ...
});

Overriding models

Point comments.models.* at your own subclasses to add behaviour. Custom classes must extend the package base (Comment / CommentReaction / CommentChangelog).

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

ssntpl/laravel-comments 适用场景与选型建议

ssntpl/laravel-comments 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 93 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 ssntpl/laravel-comments 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ssntpl/laravel-comments 我们能提供哪些服务?
定制开发 / 二次开发

基于 ssntpl/laravel-comments 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 93
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-15