简体   繁体   English

对评论实施子评论的建议(如何设置)

[英]Advices to implement child comments on comments (How to set it up)

I am working with Laravel and have created a morphable comment table with laravel.我正在与 Laravel 合作,并使用 Laravel 创建了一个可变形的评论表。 Now I want to implemnt the reply function so people can answer to comments.现在我想实现回复功能,以便人们可以回复评论。 I only do need the first level of answering.我只需要第一级的回答。 So, you can answer a comment but you cannot answer on a comment child.因此,您可以回答评论,但不能回答评论子项。 There is only one level of comment childs and not 2 or 3 like on Facebook. Facebook 上只有一级评论孩子,而不是 2 或 3 级评论。

My question to you now is, which is the best way to solve this.我现在要问你的问题是,这是解决这个问题的最佳方法。 Because in my laravel view blade I am looping through each comment and want to print my child comments on this commment to (I just don't know how do to his?).因为在我的 Laravel 视图刀片中,我正在遍历每个评论,并想将我的孩子对此评论的评论打印到(我只是不知道如何处理他的?)。

So basically, I would like to know how you would design the table, how you would map it and how you would print it in the view.所以基本上,我想知道你将如何设计表格,如何映射它以及如何在视图中打印它。

Here is my table so far:到目前为止,这是我的表:

CREATE TABLE `comments` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `commentable_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `commentable_id` bigint(20) unsigned NOT NULL,
  `user_id` bigint(20) unsigned NOT NULL,
  `post_status_id` bigint(20) unsigned NOT NULL DEFAULT '4',
  `content` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `comments_commentable_type_commentable_id_index` (`commentable_type`,`commentable_id`),
  KEY `comments_user_id_index` (`user_id`),
  KEY `comments_post_status_id_index` (`post_status_id`),
  CONSTRAINT `comments_post_status_id_foreign` FOREIGN KEY (`post_status_id`) REFERENCES `post_statuses` (`id`) ON DELETE CASCADE,
  CONSTRAINT `comments_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Actually, you have to just simply two tables if you wanna just 'one level answering'.实际上,如果您只想“一级回答”,则只需简单的两张桌子。

First one is 'comment_answer' to contain comments and answers.第一个是“comment_answer”,包含评论和答案。 No extra tables needed for comments and answers because each comment will have one answer, isn`it?评论和答案不需要额外的表格,因为每个评论都有一个答案,是吗? So, we can keep each answer in the same record as the question to which it relates.因此,我们可以将每个答案保存在与其相关的问题相同的记录中。 Migration like this;像这样迁移;

 Schema::create('comment_answer', function (Blueprint $table) {
        $table->increments('id');
        $table->longText('content');
        $table->longText('answer')->nullable();
        $table->boolean('is_publish')->default(0);
        $table->timestamps();
    });

I added columns in simple form, you can add according to your own project..我以简单的形式添加了列,您可以根据自己的项目添加..

The second one is 'post_comment_user' for contain post`s and user's relationship with comments and answers.第二个是“post_comment_user”,用于包含帖子和用户与评论和答案的关系。 Migration like this像这样迁移

Schema::create('post_comment_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id')->unsigned();
        $table->integer('comment_id')->unsigned();
        $table->integer('user_id')->unsigned();

        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
        $table->foreign('comment_id')->references('id')->on('comment_answer')->onDelete('cascade');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

    });

I've simply set up the relationship, you can also use the 'comment_answer' table for users if you want我已经简单地建立了关系,如果需要,您也可以使用用户的“comment_answer”表

Then, you can bring comments for the post in your model file ('post') like this;然后,您可以像这样在模型文件('post')中为帖子添加评论;

public function get_comments(){
    return $this->belongsToMany('App\Models\comment_answer',
        'post_comment_user',
        'post_id',
        'comment_id')
        ->where('is_publish',1)
        ->orderBy('created_at');

and controller like this;和这样的控制器;

$posts=post::where('slug',$post_slug)
        ->with('get_comments')
        ->where('is_publish',1)
        ->firstOrFail();

and blade file like this;和刀片文件这样;

@foreach($posts->get_comments as $comment)
      {{$comment->content}}
      {{$comment->answer}}
@endforeach

Child comments should just be more comments except they are mapped to another comment.子评论应该只是更多的评论,除非它们被映射到另一个评论。

Create a comment_children table.创建一个 comment_children 表。

Structure should be:结构应该是:

id: BIGINT comment_id: BIGINT child_id: BIGINT id:BIGINT comment_id:BIGINT child_id:BIGINT

Then you can create a hasManyThrough children relationship on your Comment model and use the comment_children table to match them.然后你可以在你的 Comment 模型上创建一个 hasManyThrough 子关系并使用 comment_children 表来匹配它们。

This will allow you to eager load the child comments, you can make your mode always do this by adding:这将允许您预先加载子评论,您可以通过添加以下内容使您的模式始终执行此操作:

protected $with = [‘children’]

You will also want a comments relationship on your PostStatus model.您还需要在 PostStatus 模型上建立评论关系。

Set up your model to always load this relationship also by adding:通过添加以下内容,将您的模型设置为始终加载此关系:

protected $with = [‘comments’]

Now whenever you fetch a post, the collection will always contain all the comments and all the children.现在,每当您获取帖子时,该集合将始终包含所有评论和所有子项。

Now to access it, something like:现在访问它,例如:

$post = Post::where(‘id’, 1)->first()

And in your blade:在你的刀片中:

@foreach($post->comments as $comment)
    // display comment
    @foreach($comment->children as $child)
        // display child comments
    @endforeach
@endforeach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM