定制 beyondcode/laravel-comments 二次开发

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

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

beyondcode/laravel-comments

Composer 安装命令:

composer require beyondcode/laravel-comments

包简介

Add comments to your Laravel application

README 文档

README

Latest Version on Packagist Total Downloads

Add the ability to associate comments to your Laravel Eloquent models. The comments can be approved and nested.

$post = Post::find(1);

$post->comment('This is a comment');

$post->commentAsUser($user, 'This is a comment from someone else');

Installation

You can install the package via composer:

composer require beyondcode/laravel-comments

The package will automatically register itself.

You can publish the migration with:

php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="migrations"

After the migration has been published you can create the media-table by running the migrations:

php artisan migrate

You can publish the config-file with:

php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="config"

Usage

Registering Models

To let your models be able to receive comments, add the HasComments trait to the model classes.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use BeyondCode\Comments\Traits\HasComments;

class Post extends Model
{
    use HasComments;
    ...
}

Creating Comments

To create a comment on your commentable models, you can use the comment method. It receives the string of the comment that you want to store.

$post = Post::find(1);

$comment = $post->comment('This is a comment from a user.');

The comment method returns the newly created comment class.

Sometimes you also might want to create comments on behalf of other users. You can do this using the commentAsUser method and pass in your user model that should get associated with this comment:

$post = Post::find(1);

$comment = $post->commentAsUser($yourUser, 'This is a comment from someone else.');

Approving Comments

By default, all comments that you create are not approved - this is just a boolean flag called is_approved that you can use in your views/controllers to filter out comments that you might not yet want to display.

To approve a single comment, you may use the approve method on the Comment model like this:

$post = Post::find(1);
$comment = $post->comments->first();

$comment->approve();

Auto Approve Comments

If you want to automatically approve a comment for a specific user (and optionally model) you can let your User model implement the following interface and method:

namespace App\Models;

use BeyondCode\Comments\Contracts\Commentator;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements Commentator
{
    /**
     * Check if a comment for a specific model needs to be approved.
     * @param mixed $model
     * @return bool
     */
    public function needsCommentApproval($model): bool
    {
        return false;
    }

}

The needsCommentApproval method received the model instance that you want to add a comment to and you can either return true to mark the comment as not approved, or return false to mark the comment as approved.

Retrieving Comments

The models that use the HasComments trait have access to it's comments using the comments relation:

$post = Post::find(1);

// Retrieve all comments
$comments = $post->comments;

// Retrieve only approved comments
$approved = $post->comments()->approved()->get();

Nesting Comments

BeyondCode\Comments\Comment itself implements the HasComments trait, so you can comment on a comment and therefore nest them:

$comment = BeyondCode\Comments\Comment::first();
$comment->commentAsUser($user, "Hey there!");

Deleting Replies

When you delete a comment, you may optionally want to delete all its nested comments (replies). To optionally enable this feature, set the delete_replies_along_comments config property in the config/comments.php file to true.

Events

When a new comment is added the BeyondCode\Comments\Events\CommentAdded event will be dispatched. When a comment is deleted the BeyondCode\Comments\Events\CommentDeleted event will be dispatched.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker.

Credits

License

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

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

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

它主要适用于以下技术方向: 「comments」 「beyondcode」 「laravel-comments」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 435.11k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 608
  • 点击次数: 21
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 605
  • Watchers: 13
  • Forks: 94
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-03