这是我参与11月更文挑战的第1天,活动详情查看:2021最后一次更文挑战
一、评价详情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的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。