定制 alfonsobries/laravel-commentable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

alfonsobries/laravel-commentable

Composer 安装命令:

composer require alfonsobries/laravel-commentable

包简介

A Laravel package to add comments to any model

README 文档

README

A Laravel package to add comments to any model

Stable Version License PHP Version Require

Use

Installation

  1. Install the composer package:
composer require alfonsobries/laravel-commentable
  1. Publish the database migration
php artisan vendor:publish --provider="Alfonsobries\LaravelCommentable\LaravelCommentableServiceProvider" --tag="migrations"
  1. Optionally publish the config file
php artisan vendor:publish --provider="Alfonsobries\LaravelCommentable\LaravelCommentableServiceProvider" --tag="config"
  1. Configure the model that is going to receive the comments with the CommentableInterface contract and add the Commentable trait.
<?php

namespace App\Models;

use Alfonsobries\LaravelCommentable\Contracts\CommentableInterface;
use Alfonsobries\LaravelCommentable\Traits\Commentable;
use Illuminate\Database\Eloquent\Model;


class BlogPost extends Model implements CommentableInterface
{
    use Commentable;
    // ...
}
  1. Add the CanCommentInterface contract and the CanComment trait to the model that is going to add the comment (usually the User Model). Since you can add anonymous comments this is only necessary if you want to accept User comments.
<?php

namespace App\Models;

use Alfonsobries\LaravelCommentable\Contracts\CanCommentInterface;
use Alfonsobries\LaravelCommentable\Traits\CanComment;
use Illuminate\Database\Eloquent\Model;


class User extends Model implements CanCommentInterface
{
    use CanComment;
    // ...
}

Add comments

  1. Use the addComment method to add an anonymous comment
$blogPost = BlogPost::first();

$comment = $blogPost->addComment('my comment');
  1. Use the addCommentFrom method to add a comment from a User (or the model that implements the CanCommentInterface contract)
$user = User::first();
$blogPost = BlogPost::first();

$comment = $blogPost->addCommentFrom($user, 'my comment');
  1. You can also comment with the User model (or the model that implements the CanCommentInterface contract) by using the comment method.
$user = User::first();
$blogPost = BlogPost::first();

$comment = $user->comment($blogPost, 'my comment');

Add replies to comments

  1. The Comment method is another commentable instance, meaning you can use the addComment or addCommentFrom method to add a reply.

  2. You can also use the reply and replyFrom methods that are just an alias for the comment methods above.

$user = User::create([...]);
$user2 = User::create([...]);
$blogPost = BlogPost::first();

$comment = $blogPost->commentFrom($user, 'my comment');
$comment->replyFrom($user2, 'a reply');
  1. Use the addCommentFrom method to add a comment from the User (or the model that implements the CanCommentInterface contract)
$user = User::first();
$blogPost = BlogPost::first();

$comment = $blogPost->addCommentFrom($user, 'my comment');
  1. You can also comment with the User model (or the model that implements the CanCommentInterface contract) by using the comment method.
$user = User::first();
$blogPost = BlogPost::first();

$comment = $user->comment($blogPost, 'my comment');

Get comments

  1. You can get all the user comments with the comments method
$comments = User::first()->comments();
  1. You can get all the comments associated with the commentable model with the comments method
$comments = BlogPost::first()->comments();

Sort comments

Sort the comments by popularity (average positive likes) by using the popular and unpopular scope methods.

$popularComments = BlogPost::first()->comments()->popular()->get();

$unpopularComments = BlogPost::first()->comments()->unpopular()->get();

Update comments

Since the Comment object is just a regular Eloquent Model you can use any of the different ways to update the models.

$comment->update(['comment' => 'updated comment']);

Approving comments

By default, all the new comments are unapproved (approved_at=null), meaning that you need to approve them manually based on your specific needs (you can add an event listener based on the the events listed below to do that). If you don't need to handle approved and unapproved comments, you can simply ignore the filter when querying the comments.

$comment->approve();
$comment->unapprove();

You can filter the approved or not approved comments with the approved and notApproved methods.

$model->comments()->approved()->get();
$model->comments()->notApproved()->get();

Events

The Comment model fires the following events that you can listen:

  • CommentCreated
  • CommentCreating
  • CommentUpdated
  • CommentUpdating
  • CommentDeleted
  • CommentDeleting
  • CommentSaved
  • CommentSaving

Development

Analyze the code with phpstan

composer analyse

Refactor the code with php rector

composer refactor

Format the code with php-cs-fixer

composer format

Run tests

composer test

Security

If you discover a security vulnerability within this package, please write trough the https://alfonsobries.com contact form. All security vulnerabilities will be promptly addressed.

Credits

This project exists thanks to all the people who contribute.

License

MIT © Alfonso Bribiesca

alfonsobries/laravel-commentable 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-10