webazin / comment
Laravel 5 的评论系统
v1.0
2018-05-02 13:44 UTC
Requires
- php: >=5.5.9
- illuminate/support: ~5.0
This package is auto-updated.
Last update: 2024-09-14 17:37:25 UTC
README
Laravel 评论
laravel 5 的评论系统
安装
首先,通过 Composer 引入此包。
composer require webazin/Comment
或者在项目的 composer.json 文件中添加以下内容。
"require": {
"webazin/Comment": "^1.0",
}
然后,在 app/config/app.php
中包含服务提供者。
'providers' => [ webazin\Comment\CommentServiceProvider::class ];
入门指南
包正确安装后,您需要生成迁移。
php artisan comment:migration
它将生成 <timestamp>_create_comments_table.php
迁移。您现在可以使用 artisan migrate 命令运行它
php artisan migrate
迁移后,将出现一个新的表,名为 comments
。
使用方法
设置模型
<?php namespace App; use webazin\comment\Traits\Commentable as Comment; use Illuminate\Database\Eloquent\Model; class Post extends Model implements Comment { use Comment; }
创建评论
$user = User::first(); $post = Post::first(); $comment = $post->comment([ 'comment' => 'comment text' ], $user); dd($comment);
创建或更新唯一评论
$user = User::first(); $post = Post::first(); $comment = $post->commentUnique([ 'comment' => 'comment text' ], $user); dd($comment);
更新评论
$comment = $post->updateComment(1, [ 'comment' => 'comment text' ]);
删除评论
$post->deleteComment(1);
获取评论总数
$post->commentCount // $post->commentCount() also works for this.