一、评价详情api
1.1 评价详情控制器方法
public function show(Comment $comment)
{
return $this->response->item($comment, new CommentTransformer());
}

1.2 测试效果
 
二、商家回复api
2.1 商家回复控制器方法
public function reply(Request $request, Comment $comment)
{
$request->validate([
'reply' => 'required|max:255',
], [
'reply.required' => '回复不能为空',
'reply.max' => '回复不能超过255字符'
]);
$comment -> reply = $request->input('reply');
$comment -> save();
return $this->response->noContent();
}

2.2 测试结果
 
三、优化
当用户给商品评论时(用户去请求用户端的接口,暂未处理,现在只是模拟了用户的数据。)当展示商品列表的时候也想去显示这些评论,所以给商品列表增加transformer : 

接着在Good.php 模型中加入关联:
public function comments() {
return $this->hasMany(Comment::class, 'goods_id', 'id');
}
  可以看到我们在取商品列表的时候,就有了这个商品相关的评论了。当然了,你需要加上include ,当不需要时,不需传入就可。
在学习的php的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。
|