Create Your Own Multilevel Nested Comments System in Laravel

Hey Artisan

Hope you are doing well in Laravel. Today i am coming with a brand new tutorial. Today i am going to discuss about nested comment and reply system in Laravel 7. Hope you will enjoy it.

A Comment system is a key feature of any blog or any website. So in this example tutorial i am going to show you how we can create comment and reply system in Laravel 7 application. We will see it from scratch.

Many developer looks for a good package for comment and reply system, but now we are going to create it without any packages. I will create very simple comment system where you can add post and then you can leave a comment and can make a reply to any specific post.

I will make polymorphic relationship to create this comment and reply system. Having completed this tutorial you won't need to use any packages like Facebook comments plugin, or discuss comment plugin to your website.

You can make your own comment system to your website by following this tutorial. I will create a post table and a comment table. So let's start our brand new tutorial nested comment system in laravel 7.

Preview : Add Post Page

nested-comment-and-reply-system-laravel-7

Preview : Posts Page

laravel-7-comment-and-reply-system

Preview : Single Post with Nested Comment Page

laravel-comment-and-reply-system-preview

 

One more thing, you have to make it professional. So now let's start comment and reply system in laravel.

Step 1 : Install Laravel  7

Now download laravel 7 to start our project. So run below command to download it.

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: Create Post and Comment Model

We need Post and Comment model to create comment and reply system. So let's create it. Run below command 

php artisan make:model Post -m

 

Define the schema in the post migration file.

   public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }

 

Also, we need to create Comment model and migration file, so create using the following command.

php artisan make:model Comment -m

Having done it, Now i will do the polymorphic relationship between those models. So i need to define the schema below way

public function up()
{
    Schema::create('comments', function (Blueprint $table) {
       $table->increments('id');
       $table->integer('user_id')->unsigned();
       $table->integer('parent_id')->unsigned();
       $table->text('comment');
       $table->integer('commentable_id')->unsigned();
       $table->string('commentable_type');
       $table->timestamps();
    });
}

 

Now run this below command to migrate our post and comment table.

php artisan migrate

 

Step 3:  Create Auth

As only authenticated user can leave a comment. So we need to create auth in our application. So let's make it.You need to follow some few steps to complete auth in your laravel 7 application. First you need to install laravel/ui package.

composer require laravel/ui

Now you can use following commands for creating auth:

php artisan ui bootstrap --auth

 

Step 4:  Create Relationship

Now, we need to define the Polymorphic relationships and other relationship which will be needed in this tutorial. So do it. 

app/Post.php


namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable')->whereNull('parent_id');
    }

}

 

app/Comment.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{   

    protected $guarded = [];
    
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    
    public function replies()
    {
        return $this->hasMany(Comment::class, 'parent_id');
    }
}

 

app/User.php 

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    public function posts() {
  
        return $this->hasMany(Post::class);
     
    }
}

 

Step 5: Create Controller

In this step, now we should create new controller as PostController and CommentController. So run bellow command and create new controller

php artisan make:controller PostController

and now paste this below code to your post controller.

app/Http/Controllers/PostController.php

namespace App\Http\Controllers;

use App\Post;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PostController extends Controller
{
    public function __construct()
    {
       $this->middleware('auth');
    }
    
    public function index()
    {
       $posts = Post::take(5)->get();

       return view('post.index', compact('posts'));
    }

    public function create()
    {
        return view('post.create');
    }

    public function store(Request $request)
    {

        $validator = Validator::make($request->all(), [
            'title' => 'required|min:3',
        ]);

        if ($validator->fails()) {

            return redirect('post')
                        ->withErrors($validator)
                        ->withInput();
        }

        Post::create([
            'title' => $request->title,
            'slug' => \Str::slug($request->title)
        ]);

        return redirect()->back();

    }

    public function show(Post $post) {

        return view('post.single',compact('post'));

    }
}

 

And now we have to create our comment controller. So now create it.

php artisan make:controller CommentController

and now paste this below code to your comments controller.

app/Http/Controllers/CommentController.php

namespace App\Http\Controllers;

use App\Post;
use App\Comment;
use Illuminate\Http\Request;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $comment = new Comment;

        $comment->comment = $request->comment;

        $comment->user()->associate($request->user());

        $post = Post::find($request->post_id);

        $post->comments()->save($comment);

        return back();
    }

    public function replyStore(Request $request)
    {
        $reply = new Comment();

        $reply->comment = $request->get('comment');

        $reply->user()->associate($request->user());

        $reply->parent_id = $request->get('comment_id');

        $post = Post::find($request->get('post_id'));

        $post->comments()->save($reply);

        return back();

    }
}

 

Step 6:  Create Routes

Now we have to create our routes to view our comment and reply file. Paste this below code to your web.php.

routes/web.php

Route::get('post', '[email protected]')->name('post.create');
Route::post('post', '[email protected]')->name('post.store');
Route::get('/posts', '[email protected]')->name('posts');
Route::get('/article/{post:slug}', '[email protected]')->name('post.show');
Route::post('/comment/store', '[email protected]')->name('comment.add');
Route::post('/reply/store', '[email protected]')->name('reply.add');

 

Step 7:  Create Blade Files

In last step. In this step we have to create blade files. So we have to create post folder and in the post folder we will create our view file.

resources/views/post/index.blade.php

 

resources/views/post/create.blade.php

 

resources/views/post/single.blade.php

 

resources/views/post/partials/replys.blade.php

 

Read also : Laravel Signature Pad Example Tutorial from Scratch

Now we are ready to run check our nested comment system application with laravel 7.  Now you can open bellow URL on your browser:

http://localhost:8000/post

finally our nested laravel comment system in laravel 7 is over. Hope it can help you. If you face any error then please leave a response.

 

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website