laravelcity/laravel-comments

这是一个用于管理评论的包

1.0.1 2018-10-21 10:29 UTC

This package is auto-updated.

Last update: 2024-09-21 22:45:51 UTC


README

这是一个用于管理评论的包。

安装laravel comments

步骤1

在您的终端运行以下语句

composer require laravelcity/laravel-comments

步骤2

config/app.php 中添加 providerfacade

    providers => [
       ...
       Laravelcity\Comments\CommentsServiceProvider::class,
    ],


    aliases => [
       ...
       'Comments' => \Laravelcity\Comments\Facade\Comment::class,
    ]

步骤3

php artisan vendor:publish --provider=Laravelcity\Comments\CommentsServiceProviderphp artisan vendor:publish

现在您可以在文件 config/comments.php 中自定义配置

步骤4

    php artisan migrate

步骤5

请将 Laravelcity\Comments\Models\CanComment 添加到 user 模型

    class User extends Authenticatable
    {
        use CanComment;
        .
        .
        .

步骤6

请将 Laravelcity\Comments\Models\Commentable 添加到您希望启动其评论系统的任何模型。

例如,为帖子模型添加 commentable

    class Post extends Model
    {
        use Commentable;

如何使用Comments

通过依赖注入定义评论方法

    protected $comment;
    
     public function __construct()
     {
         $this->comment=Comment::start();
         
         //or
         
         $this->comment=Comment::start(CustomComment::class); //set custom model for comments
     }
    

插入评论

   auth()->user()->comment(
            $model, //for example => App\Models\Posts
            $request->get('content'),
            $request->get('parent'),
            $status // default is pending but change to 'accepted','rejected','pending'
    );
    

获取评论

   public function show($id){
        $comment=$this->comment->find($id);
        .
        .
        .
    }
    

获取所有评论

   public function index(){
        //return all comments
        $comments=$this->comment->all();
        
        // or
      
        //return user comments
        $comments=auth()->user()->comments()->paginate();

        .
        .
        .
    }
    

更新评论

    public function update($id){
        $comment=$this->comment->find($id);
        $comment->update(request()->only(['content']));
        .
        .
        .
    }

删除评论

     public function destroy($id){
        $comment=$this->comment->find($id);
        $comment->delete();
        .
        .
        .
    }

评论操作

   public function actions()
    {
        try {
            $this->comment->runActions();
            // enter code ..

        }catch (CommentException $e){
            return redirect()->back()->withErrors($e->getMessage());
        }
    }

$this->comment->runActions(); 它有四个操作符

  • 删除
  • 删除
  • 恢复
  • 状态

要运行这些方法,发送以下参数

  • 动作

    这是一个用于保存所选操作符的参数。参数是字符串,仅限于 '> 'destroy','delete','restore','status'

  • 选择

    这是一个用于保存要执行操作的所选评论的参数。参数是数组,例如 '> [2,5,8,9]

注意:对于运行状态操作,必须通过 value 输入发送状态值,并使用以下变量之一 '> 'accepted','rejected','pending'

设置新的评论模型

必须创建新的模型并扩展 Laravelcity\Comments\Models\Comments

    use Laravelcity\Comments\Models\Comments;

    class CustomComment extends Comments
    {

        public function __construct(array $attributes = [])
        {
            parent::__construct($attributes);
        }

    }

现在使用以下

  public function __construct()
    {
        $this->comment=Comment::start(CustomComment::class); //set custom model for comment facade
    }

请注意,您可以使用不同的模型创建多个分类