- 创建评论Migration
- 创建评论表结构
- 创建评论表模型
- 实现评论API
- 添加评论API
- 查看评论API
- 删除评论API
1. 创建评论Migration
php artisan make:migration create_table_comments --create=comments
2. 创建评论表结构
increments('id');
$table->timestamps();
//自定义字段
$table->text('content')->comment('评论内容');
$table->unsignedInteger('user_id')->comment('评论人');
$table->unsignedInteger('question_id')->nullable()->comment('问题编号');//可为空,由于无法确认是针对问题评论
$table->unsignedInteger('answer_id')->nullable()->comment('回答编号');//可为空,由于无法确认是针对回答评论
$table->unsignedInteger('reply_to')->nullable()->comment('回复评论');
//外键关系
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('question_id')->references('id')->on('questions');
$table->foreign('answer_id')->references('id')->on('answers');
$table->foreign('reply_to')->references('id')->on('comments');
});
}
public function down()
{
Schema::dropIfExists('comments');
}
}
版本2
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->text('content')->comment('内容');
$table->unsignedInteger('user_id')->default('0')->comment('评论人');
$table->foreign('user_id')->references('id')->on('users');
$table->unsignedInteger('question_id')->nullable()->comment('问题');
$table->foreign('question_id')->references('id')->on('questions');
$table->unsignedInteger('answer_id')->nullable()->comment('回答');
$table->foreign('answer_id')->references('id')->on('answers');
$table->unsignedInteger('reply_id')->nullable()->comment('回复评论');
$table->foreign('reply_id')->references('id')->on('comments');
$table->timestamps();
});
}
注意:reply_id 是针对评论而发表的评论,因此外键连接的位置仍然是 comments 表。
php artisan migrate --pretend
php artisan migrate:rollback
php artisan migrate
3. 创建评论表模型
php artisan make:model Model\Comment
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
}
4.实现评论API
路由文件中提取公共函数
function commentInstance(){
return new App\Model\Comment;
}
4.1 添加评论API
创建路由
Route::any('/api/comment/add',function(){
return commentInstance()->add();
});
实现方法
/*添加评论API*/
public function add()
{
//检查用户是否登录
if(!userInstance()->isLogin()){
return ['err'=>1, 'msg'=>'请先登录'];
}
//判断是否具有评论内容
if(!rq('content')){
return ['err'=>1, 'msg'=>'请先评论'];
}
//判断是对问题评论或是对回答评论
if(!rq('question_id') && !rq('answer_id')){
return ['err'=>1, 'msg'=>'请针对问题或回答进行评论'];
}
//判断是否同时对问题或答案评论
if(rq('questioin_id') && rq('answer_id')){
return ['err'=>1, 'msg'=>'请勿针对问题和答案同时评论'];
}
//若针对问题评论
if(rq('question_id')){
//判断问题是否存在
$question = questionInstance()->find(rq('question_id'));
if(!$question){
return ['err'=>1, 'msg'=>'问题不存在'];
}
$this->question_id = rq('question_id');
}
//若针对回答评论
if(rq('answer_id')){
//判断回答是否存在
$answer = answerInstance()->find(rq('answer_id'));
if(!$answer){
return ['err'=>1, 'msg'=>'回答不存在'];
}
$this->answer_id = rq('answer_id');
}
//判断是否对自身评论进行回复
if(rq('reply_to')){
$target = $this->find(rq('reply_to'));
if(!$target){
return ['err'=>1, 'msg'=>'目录评论不存在'];
}
//禁止自己回复自己
if($target->user_id == session('user.id')){
return ['err'=>1, 'msg'=>'禁止回复自身评论'];
}
}
$this->content = rq('content');
$this->user_id = session('user.id');
$this->reply_to = rq('reply_to');
//插入数据库并返回结果
return $this->save() ? ['err'=>0,'id'=>$this->id] : ['err'=>1, 'msg'=>'新增失败'];
}
# 版本2
/*API 添加评论*/
public function add()
{
//判断用户是否登录
if(!user()->islogin()){
return ['err'=>1, 'msg'=>'尚未登录'];
}
$this->user_id = session('user_id');
//判断评论内容
if(!rq('content')){
return ['err'=>1, 'msg'=>'缺少评论内容'];
}
$this->content = rq('content');
//针对问题或回答评论
if(!rq('question_id') && !rq('answer_id')){
return ['err'=>1, 'msg'=>'缺少问题编号或回答编号'];
}
if(rq('question_id') && rq('answer_id')){
return ['err'=>1, 'msg'=>'禁止同时评论问题和回答'];
}
//针对问题评论
if(rq('question_id')){
//判断问题是否存在
$question = question()->find(rq('question_id'));
if(!$question){
return ['err'=>1,'msg'=>'问题不存在'];
}
$this->question_id = rq('question_id');
}
//针对回答评论
if(rq('answer_id')){
//判断回答是否存在
$answer = answer()->find(rq('answer_id'));
if(!$answer){
return ['err'=>1, 'msg'=>'回答不存在'];
}
$this->answer_id = rq('answer_id');
}
//针对评论而评论
if(rq('reply_id')){
//判断评论是否存在
$comment = $this->find(rq('reply_id'));
if(!$comment){
return ['err'=>1, 'msg'=>'评论不存在'];
}
//禁止对自己评论进行评论
if($comment->user_id == session('user_id')){
return ['err'=>1, 'msg'=>'禁止自身评论'];
}
$this->reply_id = rq('reply_id');
}
//保存数据
return $this->save()?['err'=>0,'msg'=>'评论成功','id'=>$this->id]:['err'=>1,'msg'=>'评论失败'];
}
4.2 查看评论API
创建路由
Route::any('/api/comment/read',function(){
return commentInstance()->read();
});
实现方法
public function read()
{
//判断参数是否存在
if(!rq('question_id') && !rq('answer_id')){
return ['err'=>1, 'msg'=>'参数错误'];
}
//根据问题查看评论
if(rq('question_id')){
//判断问题是否存在
$question = questionInstance()->find(rq('question_id'));
if(!$question){
return ['err'=>1, 'msg'=>'问题不存在'];
}
//获取问题下所有评论
$data = $this->where('question_id',rq('question_id'))->get();
}
//根据回答查看评论
if(rq('answer_id')){
//判断回答是否存在
$answer = answerInstance()->find(rq('answer_id'));
if(!$answer){
return ['err'=>1, 'msg'=>'回答不存在'];
}
//获取回答下所有评论
$data = $this->where('answer_id',rq('answer_id'))->get();
}
return $data->isEmpty() ? ['err'=>1, 'msg'=>'暂无评论'] : ['err'=>0,'data'=>$data->keyBy('id')];
}
# 版本2
/*API 查看评论*/
public function read()
{
//判断参数
if(!rq('question_id') && !rq('answer_id')){
return ['err'=>1, 'msg'=>'缺少问题编号或回答编号'];
}
//查看问题的评论
if(rq('question_id')){
//判断问题是否存在
$question = question()->find(rq('question_id'));
if(!$question){
return ['err'=>1, 'msg'=>'问题不存在'];
}
//获取问题下所有评论
$comment = $this->where('question_id',rq('question_id'));
}
//查看回答下的评论
if(rq('answer_id')){
//判断回答是否存在
$answer = answer()->find(rq('answer_id'));
if(!$answer){
return ['err'=>1, 'msg'=>'回答不存在'];
}
//获取回答下所有评论
$comment = $this->where('answer_id', rq('answer_id'));
}
$data = $comment->get()->keyBy('id');
//返回评论
return $data->isEmpty() ? ['err'=>1, 'msg'=>'暂无评论'] : ['err'=>0, 'data'=>$data];
}
4.3 删除评论API
创建路由
Route::any('/api/comment/remove',function(){
return commentInstance()->remove();
});
实现方法
/*删除评论API*/
public function remove()
{
//检查用户是否登录
if(!userInstance()->isLogin()){
return ['err'=>1, 'msg'=>'请先登录'];
}
//根据评论编号删除
if(!rq('id')){
return ['err'=>1, 'msg'=>'参数错误'];
}
//判断评论是否存在
$comment = $this->find(rq('id'));
if(!$comment){
return ['err'=>1, 'msg'=>'评论不存在'];
}
//判断是否为本人评论
if($comment->user_id != session('user.id')){
return ['err'=>1, 'msg'=>'权限错误'];
}
//删除所有回复过评论的评论
$this->where('reply_to',rq('id'))->delete();
//删除评论
return $comment->delete() ? ['err'=>0, 'msg'=>'删除成功'] : ['err'=>1, 'msg'=>'删除失败'];
}
# 版本2
/*API 删除评论*/
public function del()
{
//判断用户是否登录
if(!user()->islogin()){
return ['err'=>1, 'msg'=>'尚未登录'];
}
//判断评论编号
if(!rq('id')){
return ['err'=>1, 'msg'=>'缺少评论编号'];
}
//判断评论是否存在
$comment = $this->find(rq('id'));
if(!$comment){
return ['err'=>1, 'msg'=>'评论不存在'];
}
//自身可删除
if($comment->user_id != session('user_id')){
return ['err'=>1, 'msg'=>'权限不足'];
}
//删除评论下的回复
$this->where('reply_id',rq('id'))->delete();
//删除评论
return $comment->delete()?['err'=>0,'msg'=>'删除成功']:['err'=>1,'msg'=>'删除失败'];
}