亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對評論實施子評論的建議(如何設置)

對評論實施子評論的建議(如何設置)

PHP
aluckdog 2022-07-02 16:50:50
我正在使用 Laravel,并使用 laravel 創建了一個可變形的評論表。現在我想實現回復功能,以便人們可以回復評論。我只需要第一級的回答。因此,您可以回答評論,但不能回答評論孩子。Facebook 上只有一級的評論孩子,而不是 2 或 3 級。我現在向您提出的問題是,這是解決此問題的最佳方法。因為在我的 laravel 視圖刀片中,我正在循環瀏覽每條評論,并希望將我的孩子對此評論的評論打印到(我只是不知道如何處理他的?)。所以基本上,我想知道你將如何設計表格,如何映射它以及如何在視圖中打印它。到目前為止,這是我的桌子: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;
查看完整描述

2 回答

?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

實際上,如果您只想“一級回答”,則只需兩張桌子。


第一個是“comment_answer”,包含評論和答案。評論和答案不需要額外的表格,因為每條評論都有一個答案,不是嗎?因此,我們可以將每個答案與其相關的問題保存在同一記錄中。像這樣遷移;


 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();

    });

我以簡單的形式添加了列,您可以根據自己的項目添加..


第二個是'post_comment_user',用于包含帖子和用戶與評論和答案的關系。這樣的遷移


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');


    });

我只是建立了關系,如果你愿意,你也可以為用戶使用“comment_answer”表


然后,您可以像這樣在模型文件(“帖子”)中為帖子添加評論;


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');

和這樣的控制器;


$posts=post::where('slug',$post_slug)

        ->with('get_comments')

        ->where('is_publish',1)

        ->firstOrFail();

和這樣的刀片文件;


@foreach($posts->get_comments as $comment)

      {{$comment->content}}

      {{$comment->answer}}

@endforeach


查看完整回答
反對 回復 2022-07-02
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

子評論應該只是更多評論,除非它們被映射到另一個評論。


創建一個comment_children 表。


結構應該是:


id: BIGINT comment_id: BIGINT child_id: BIGINT


然后你可以在你的 Comment 模型上創建一個 hasManyThrough 子關系,并使用 comment_children 表來匹配它們。


這將允許您急切加載子評論,您可以通過添加使您的模式始終這樣做:


protected $with = [‘children’]

您還需要在 PostStatus 模型上建立評論關系。


還可以通過添加以下內容來設置您的模型以始終加載此關系:


protected $with = [‘comments’]

現在,每當您獲取帖子時,該集合將始終包含所有評論和所有子項。


現在要訪問它,例如:


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

在你的刀片中:


@foreach($post->comments as $comment)

    // display comment

    @foreach($comment->children as $child)

        // display child comments

    @endforeach

@endforeach


查看完整回答
反對 回復 2022-07-02
  • 2 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號