【问题标题】:laravel api : nested comment and replieslaravel api:嵌套评论和回复
【发布时间】: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[]"

我应该怎么做才能不再显示回放?

【问题讨论】:

    标签: json laravel api postman


    【解决方案1】:

    所以你可以在这里做几件事,首先你的回复有commentable_type和commentable_id,而这些应该是可以为空的,因为在查看回复时你只需要知道它的父母而不是帖子,因为你可以通过这个父母。

    这将允许您的$post->comments 关系不返回对 cme​​ts 的回复。

    接下来,您可以使用点符号向您的 cmets 添加回复。

    $post = Post::with('comments.replies')->find($id);
    

    这会返回类似这样的东西

        [
            {
                "id": 1,
                "title": "Title",
                "body": "Body",
                "created_at": "2021-05-01T11:34:27.000000Z",
                "updated_at": "2021-05-01T11:34:28.000000Z",
                "comments": [
                    {
                        "id": 1,
                        "post_id": 1,
                        "parent_id": null,
                        "body": "No Replies",
                        "created_at": "2021-05-01T11:34:41.000000Z",
                        "updated_at": "2021-05-01T11:34:43.000000Z",
                        "replies": []
                    },    
                    {
                        "id": 2,
                        "post_id": 1,
                        "parent_id": null,
                        "body": "One Reply",
                        "created_at": "2021-05-01T11:35:18.000000Z",
                        "updated_at": "2021-05-01T11:35:19.000000Z",
                        "replies": [
                            {
                                "id": 3,
                                "post_id": null,
                                "parent_id": 2,
                                "body": "Reply",
                                "created_at": "2021-05-01T11:35:29.000000Z",
                                "updated_at": "2021-05-01T11:35:32.000000Z"
                            }
                        ]
                    }
                ]
            }
        ]
    

    【讨论】:

    • 谢谢你,我编辑了我的代码和问题..你能再检查一下我的问题吗?
    • 因此,从我所看到的与您的关系来看,一个更好的主意是从评论中完全删除父 ID。因为你有commentable_id 和commentable_type,所以从技术上讲,回复是评论的评论。因此,回复的 commentable_id 和 commentable_type 将是它的父评论。然后改变回复关系。试一试这段关系,如果你有问题,请再看这里,我会看看。
    猜你喜欢
    • 1970-01-01
    • 2015-09-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2021-09-09
    • 2015-04-30
    相关资源
    最近更新 更多