Laravel5 implements the form of nested comments (code details)

01-28-2024

What this article brings to you is the form (code details) of embedded comments in Laravel5, which has certain reference value. Friends who need it can refer to it and hope it will help you. We often see many forms of comments, such as' @' so-and-so, or contractive comments like Zhihu's, or nested comments, so nested comments are the most common at first, because they are more eye-catching.

preparatory work/be about to work

1. Design three tables: users, posts and comments. The table structure is as follows:

users

Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });

posts

Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->integer('user_id')->index(); $table->text('content'); $table->timestamps(); });

comments

Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->index(); $table->integer('post_id')->index(); $table->integer('parent_id')->index()->default(0); $table->text('body'); $table->timestamps(); });

2.Model layer: Post.php file.

/** * An article has multiple comments. * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function comments() { return $this->hasMany(Comment::class); } /** * get comments on this article grouped by parent_id. * @return static */ public function getComments() { return $this->comments()->with('owner')->get()->groupBy('parent_id'); }

Comments.php document

/** * The user to whom this comment belongs * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function owner() { return $this->belongsTo(User::class, 'user_id'); } /** * Sub-comments of this comment * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function replies() { return $this->hasMany(Comment::class, 'parent_id'); }

Logically writing the nested comments we want to achieve has actually been a bit of an idea in our preparation work. We first display an article, and at the same time use the one-to-many relationship between articles and comments to display all comments, but one field involved in our comments is parent_id, which is actually very special. We use this field to group. Code is the above return $ this-> comments ()-> with ('owner')-> get ()-> group by ('parent _ id'), the specific process is as follows:

Web.php document

\Auth::loginUsingId(1); //Login with user id 1 //Display articles and corresponding comments. Route::get('/post/show/{post}', function (\App\Post $post) { $post->load('comments.owner'); $comments = $post->getComments(); $comments

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us