【发布时间】:2021-07-24 10:47:48
【问题描述】:
我是 laravel 初学者,想用 laravel 8 构建一个 API。
我的帖子有一个嵌套评论和回复系统
而cmets和post之间的关系是多态的
我想将帖子、cmets 和回复显示为 JSON
但我不知道我的查询是否正确,因为我无法理解 cmets 和回复之间的关系,我只能看到与帖子相关的所有 cmets 和回复。
这是邮递员中的 JSON 响应:
[
{
"id": 45,
"category_id": 11,
"user_id": 1,
"title": "title example",
"body": "body example",
"video": null,
"study_time": "8",
"likes": null,
"status": null,
"tags": [],
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:09:14.000000Z",
"updated_at": "2021-05-01T14:09:14.000000Z",
"comments": [
{
"id": 13,
"commentable_id": 45,
"commentable_type": "App\\Models\\Post",
"parent_id": null,
"name": "sara",
"email": "sara@gmail",
"comment": "its good",
"images": null,
"like": null,
"dislike": null,
"status": null,
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:10:14.000000Z",
"updated_at": "2021-05-01T14:10:14.000000Z"
},
{
"id": 14,
"commentable_id": 45,
"commentable_type": "App\\Models\\Post",
"parent_id": 13,
"name": "john",
"email": "john@gmail.com",
"comment": "its not good",
"images": null,
"like": null,
"dislike": null,
"status": null,
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:10:50.000000Z",
"updated_at": "2021-05-01T14:10:50.000000Z"
},
{
"id": 15,
"commentable_id": 45,
"commentable_type": "App\\Models\\Post",
"parent_id": 14,
"name": "sara",
"email": "sara@gmail.com",
"comment": "why?",
"images": null,
"like": null,
"dislike": null,
"status": null,
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:11:12.000000Z",
"updated_at": "2021-05-01T14:11:12.000000Z"
}
],
}
]
如您所见,回复不在数组中,不清楚哪个是重播,哪个是评论
这是我的PostDetailsController:
class PostDetailsController extends Controller
{
/**
* Disply post and comments with replies
**/
public function showPost($id){
$postFind = Post::find($id);
if(is_null($postFind)){
return response()->json('not found' , 404);
}
$post = Post::with('comments')->find($id);
return response()->json([$post] , 200);
}
关于 cmets:
comments table:
chema::create('comments', function (Blueprint $table) {
$table->id();
$table->integer('commentable_id');
$table->string('commentable_type');
$table->unsignedBigInteger('parent_id')->nullable(); //nullable for comments , with value for replies
$table->string('name' , 45);
$table->string('email');
$table->longText('comment');
$table->foreign('parent_id')->references('id')->on('comments')->onUpdate('cascade')->onDelete('cascade');
$table->rememberToken();
$table->softDeletes();
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP'));
});
}
所以如果parent_id 是NULL,它是一个评论,如果有值(父 cmets 的 id)它是一个回复。 (并且回复的回复之前的回复 id 为 parent_id )
和模型:
Post:
class Post extends Model
{
protected $fillable = [
'user_id' ,
'category_id' ,
'title' ,
'body' ,
'study_time',
'status',
'tags',
];
public function comments()
{
return $this->morphMany(Comment::class, 'commentable' );
}
}
Comment:
class Comment extends Model
{
protected $fillable = [
'parent_id' , 'name' , 'email' , 'comment' , 'status' , 'images'
];
use HasFactory;
public function commentable(){
return $this->morphTo();
}
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id');
}
}
那么你能告诉我我的查询有误吗?
我应该怎么做才能在数组中显示回复?
编辑
我将代码更改为:
class PostDetailsController extends Controller
{
/**
* Disply post and comments with replies
**/
public function showPost($id){
$postFind = Post::find($id);
if(is_null($postFind)){
return response()->json('not found' , 404);
}
$post = Post::with('comments.replies')->find($id);
return response()->json([$post] , 200);
}
响应是:
[
{
"id": 45,
"category_id": 11,
"user_id": 1,
"title": "title example",
"body": "body example",
"video": null,
"study_time": "8",
"likes": null,
"status": null,
"tags": [],
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:09:14.000000Z",
"updated_at": "2021-05-01T14:09:14.000000Z",
"comments": [
{
"id": 13,
"commentable_id": 45,
"commentable_type": "App\\Models\\Post",
"parent_id": null,
"name": "sara",
"email": "sara@gmail",
"comment": "its good",
"images": null,
"like": null,
"dislike": null,
"status": null,
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:10:14.000000Z",
"updated_at": "2021-05-01T14:10:14.000000Z",
"replies": [
{
"id": 14,
"commentable_id": 45,
"commentable_type": "App\\Models\\Post",
"parent_id": 13,
"name": "john",
"email": "john@gmail.com",
"comment": "its not good",
"images": null,
"like": null,
"dislike": null,
"status": null,
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:10:50.000000Z",
"updated_at": "2021-05-01T14:10:50.000000Z"
}
]
},
{
"id": 14,
"commentable_id": 45,
"commentable_type": "App\\Models\\Post",
"parent_id": 13,
"name": "john",
"email": "john@gmail.com",
"comment": "its not good",
"images": null,
"like": null,
"dislike": null,
"status": null,
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:10:50.000000Z",
"updated_at": "2021-05-01T14:10:50.000000Z",
"replies": [
{
"id": 15,
"commentable_id": 45,
"commentable_type": "App\\Models\\Post",
"parent_id": 14,
"name": "sara",
"email": "sara@gmail.com",
"comment": "why?",
"images": null,
"like": null,
"dislike": null,
"status": null,
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:11:12.000000Z",
"updated_at": "2021-05-01T14:11:12.000000Z"
}
]
},
{
"id": 15,
"commentable_id": 45,
"commentable_type": "App\\Models\\Post",
"parent_id": 14,
"name": "sara",
"email": "sara@gmail.com",
"comment": "why?",
"images": null,
"like": null,
"dislike": null,
"status": null,
"remember_token": null,
"deleted_at": null,
"created_at": "2021-05-01T14:11:12.000000Z",
"updated_at": "2021-05-01T14:11:12.000000Z",
"replies": []
}
],
}
]
所以我可以在回复中看到每条评论或回复的回复,但正如您所见,回复是重复的,最后一个没有回复的回复有"replies[]"
我应该怎么做才能不再显示回放?
【问题讨论】: