betalabs / laravel-comment

为laravel项目提供的另一个评论系统。

1.0.3 2019-03-05 11:27 UTC

README

Latest Version on Packagist Software License Build Status Total Downloads

为laravel项目提供的另一个评论系统。

版本兼容性

安装

通过Composer

$ composer require actuallymab/laravel-comment

Laravel 5.5使用包自动发现,因此不需要您手动添加ServiceProvider。

如果您不使用自动发现或使用Laravel版本< 5.5,请将ServiceProvider添加到您的app.php文件中

\Actuallymab\LaravelComment\LaravelCommentServiceProvider::class

发布并迁移评论表。

$ php artisan vendor:publish
$ php artisan migrate

CanComment特性添加到您的User模型中。

use Actuallymab\LaravelComment\CanComment;

Commentable特性添加到您的可评论模型中。

use Actuallymab\LaravelComment\Commentable;

如果您想使用自己的Comment模型,创建一个新的模型并扩展我的Comment模型。

class Comment extends Actuallymab\LaravelComment\Comment
{
  ...
}

Comment包包含几种模式。

1- 如果您想让用户可以对您的模型(s)进行评分,请在您的Commentable模型中将canBeRated设置为true。

class Product extends Model {
  use Commentable;

  protected $canBeRated = true;

  ...
}

2- 如果您想批准您的可评论模型的评论,必须在您的Commentable模型中将mustBeApproved设置为true。

class Product extends Model {
  use Commentable;

  protected $mustBeApproved = true;

  ...
}

3- 您不想对所有用户的评论进行批准(比如,您只想批准自己的评论?)。那么,请给您的User模型添加一个isAdmin方法,如果用户是管理员则返回true。

class User extends Model {
  use CanComment;
  
  protected $fillable = [
    'isAdmin',
    ....
  ];

  public function isAdmin() {
    return $this->isAdmin;
  }

  ...
}

用法

$user = App\User::find(1);
$product = App\Product::find(1);

// $user->comment(Commentable $model, $comment = '', $rate = 0);
$user->comment($product, 'Lorem ipsum ..', 3);

// approve it -- if you are admin or you don't use mustBeApproved option, it is not necessary
$product->comments[0]->approve();

// get avg rating -- it calculates approved average rate.
$product->averageRate();

// get total comment count -- it calculates approved comments count.
$product->totalCommentCount();

变更日志

有关最近变更的更多信息,请参阅CHANGELOG

测试

$ composer test

贡献

有关详细信息,请参阅CONTRIBUTINGCONDUCT

安全性

如果您发现任何与安全性相关的问题,请通过电子邮件mehmet.aydin.bahadir@gmail.com联系,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件