Create Multiple Comment in Laravel 5.8

Xây dựng Multiple Comment trong Laravel, cũng như bài trước mình có chia sẻ với mọi người cách tạo (Create Multiple Subcategories in Laravel 5.8Multiple Subcategories in Laravel 5.8 + Vue) thì trong bài này cũng tương đối giống vậy.

Đầu tiên chúng ta cần cài đặt Laravel 5.8

# composer global require laravel/installer
or 
# composer create-project --prefer-dist laravel/laravel blog "5.8.*"

Sau khi cài đặt và tạo project thành công! Ta tiến hành tìm hiểu các mối liên kết giữa Post và Comment với User như thế nào? Chúng ta cũng biết rằng!

+ Post -> (hasMany) Comments, Post -> (belongsTo) User

//App\Post
    public function user(){
        return $this->belongsTo(User::class);
    }
    public function comments() 
    {
    return $this->hasMany('App\Comment');
    }

+ Comment -> (belongsTo) Post và User

//App\Comment 
    public function user()
    {
    return $this->belongsTo('App\User');
    }

    public function post()
    {
        return $this->belongsTo(Post::class);
    }

+ User -> (hasMany) Comments, User -> (hasMany) Posts

//App\User 
    public function posts(){
        return $this->hasMany(Post::class);
    }
    public function comments() 
    {
        return $this->hasMany('App\Comment');
    }

Chúng ta đã liên kết mối quan hệ giữa các table(Posts,Users,Comments), giờ chúng ta cần tạo migration database của Posts và Comments như sau

# php artisan make:migration create_posts_table --create=posts

Schema::create('posts', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('title');
    $table->string('keyword');
    $table->string('des');
    $table->string('slug');
    $table->string('image');
    $table->string('content');
    $table->timestamps();
});

# php artisan make:migration create_comments_table --create=comments

Schema::create('comments', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->integer('user_id')->unsigned();
    $table->integer('post_id')->unsigned();
    $table->text('body');
    $table->timestamps();
});

Bên trên chúng ta chưa thấy các khóa ngoại của chúng liên kết, giờ ta chỉnh sửa lại table posts như sau:

# php artisan make:migration create_user_post_table --create=user_post

public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('posts', function(Blueprint $table)
        {
            $table->dropForeign(['user_id']);
        });
    }

Chúng ta đã add một user_id vào table posts, để thể hiện mối liện hệ của  table Users và Posts, giờ ta cần chỉnh sửa lại table Comments xíu, vì để có được multiple comments ta cần phải xác định thêm một cột comment_id vào table comments

# php artisan make:migration create_add_column_comments_table --create=comments

public function up()
    {
        Schema::table('comments', function (Blueprint $table) {
            $table->integer('comment_id')->after('post_id')->unsigned();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comments');
    }

Bên cạnh đó ta cần chỉnh sửa lại file App\Comment.php của ta, thêm vào hai phương thức sau:

 public function comments()
    {
        return $this->hasMany(Comment::class,'comment_id');
    }
    public function childrenComments()
    {
        return $this->hasMany(Comment::class,'comment_id')->with('childrenComments')->with('user');
    }

Trong App\Http\CommentControler.php, Nếu bạn muốn hiện thị post_id nào thì chèn tham số vào function

/**
* GET POST COMMENT
*/
public function getPostComment($post_id){
  
   $comment = Comment::with('user')->where("post_id",$post_id)->where("comment_id",0)->with('childrenComments')->get()->toArray();
  // dd($comment);
   //return Response()->json($comment);
   return View('comment')->with(array('comment'=>$comment));
}

Trong Views/comment.blade.php bạn chỉ việc show nội dung ra, bạn có thể tạo một childrenComment và include nó vào foreach để show ra multiple comment

@foreach($comment as $item)
    //show comment 
    //or
    //includes('childrenComments',['childrenComments'=>$item->childrenComments])
@foreach

Comment trên website:https://hoanguyenit.com mình cũng sữ dụng cách trên, nhưng mình kết hợp giữa Laravel + Vue làm cho comment multiple của mình!

Bạn có thể xem lại phần tạo: Create Multiple Subcategories in Laravel 5.8

Bài Viết Liên Quan

Messsage

Ủng hộ tôi bằng cách click vào quảng cáo. Để tôi có kinh phí tiếp tục phát triển Website!(Support me by clicking on the ad. Let me have the money to continue developing the Website!)