In a Laravel 11 application, you can easily create a comment and reply system with the help of Eloquent relationships.
Let’s see how to create a comment and reply system in a Laravel application:
Step 1 – Create Post and Comment Migration File
Run the following command to create post and comment migration file with model:
php artisan make:model post -m
php artisan make:model comment -m
Edit database/migrations/posts.php
file, and add the following code in it:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
$table->softDeletes();
});
Edit database/migrations/comments.php
file, and add the following code in it:
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->integer('parent_id')->unsigned()->nullable();
$table->text('body');
$table->timestamps();
$table->softDeletes();
});
Step 2 – Define Relationship Between Tables
Edit app/models/posts.php
file, and define relationship between posts and comments table:
use SoftDeletes;
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['title', 'body'];
/**
* The has Many Relationship
*
* @var array
*/
public function comments()
{
return $this->hasMany(Comment::class)->whereNull('parent_id');
}
Edit app/models/comments.php
file, and define relationship between comments and users table:
use SoftDeletes;
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['user_id', 'post_id', 'parent_id', 'body'];
/**
* The belongs to Relationship
*
* @var array
*/
public function user()
{
return $this->belongsTo(User::class);
}
/**
* The has Many Relationship
*
* @var array
*/
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id');
}
Step 3 – Set Up Bootstrap Auth
Run the following command to install laravel/ui in application:
composer require laravel/ui
Run the following command to generate authentication system using bootstrap ui auth command in application:
php artisan ui bootstrap --auth
Compile the bootstrap authentication file by running the following command:
npm install
npm run
Step 4 – Create Controller
Run the following command to create controller class and define methods in it to handle comments and replies:
php artisan make:controller PostController
Edit app/Http/Controllers/PostController.php
file, and define methods in it:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$post = Post::find($id);
return view('posts.show', compact('post'));
}
}
Run the following command to create comment controller class:
php artisan make:controller CommentController
Edit app/Http/Controllers/CommentController.php
file, and define methods in it:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Comment;
class CommentController extends Controller
{
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'body'=>'required',
]);
$input = $request->all();
$input['user_id'] = auth()->user()->id;
Comment::create($input);
return back();
}
}
Step 5 – Define Routes
Edit routes/web.php file, and add the following routes in it:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
use App\Http\Controllers\CommentController;
Route::get('/posts', [PostController::class, 'index'])->name('posts.index');
Route::get('/posts/{id}', [PostController::class, 'show'])->name('posts.show');
Route::post('/comments', [CommentController::class, 'store'])->name('comments.store');
Step 6 – Create Views
Go to resources/views
directory and create some views to display posts, comment form and comments list.
Create index.blade.php
file, and add the following code in it to display all posts with show posts button:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<h1>Post List</h1>
<a href="{{ route('posts.create') }}" class="btn btn-success" style="float: right">Create Post</a>
<table class="table table-bordered">
<thead>
<th width="80px">Id</th>
<th>Title</th>
<th width="150px">Action</th>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{ $post->id }}</td>
<td>{{ $post->title }}</td>
<td>
<a href="{{ route('posts.show', $post->id) }}" class="btn btn-primary">View Post</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@endsection
Create show.blade.php
file, and add the following code in it to display single post with comment form:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-body">
<h3 class="text-center text-success">Details</h3>
<br/>
<h2>{{ $post->title }}</h2>
<p>
{{ $post->body }}
</p>
<hr />
<h4>Display Comments</h4>
@include('posts.commentsDisplay', ['comments' => $post->comments, 'post_id' => $post->id])
<hr />
<h4>Add comment</h4>
<form method="post" action="{{ route('comments.store') }}">
@csrf
<div class="form-group">
<textarea class="form-control" name="body"></textarea>
<input type="hidden" name="post_id" value="{{ $post->id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" value="Add Comment" />
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Create commentsDisplay.blade.php
file, and add the following code in it to display all comments related to post:
@foreach($comments as $comment)
<div class="display-comment" @if($comment->parent_id != null) style="margin-left:40px;" @endif>
<strong>{{ $comment->user->name }}</strong>
<p>{{ $comment->body }}</p>
<a href="" id="reply"></a>
<form method="post" action="{{ route('comments.store') }}">
@csrf
<div class="form-group">
<input type="text" name="body" class="form-control" />
<input type="hidden" name="post_id" value="{{ $post_id }}" />
<input type="hidden" name="parent_id" value="{{ $comment->id }}" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-warning" value="Reply" />
</div>
</form>
@include('posts.commentsDisplay', ['comments' => $comment->replies])
</div>
@endforeach
Step 7 – Test This Application
Run the following command to start application server:
php artisan serve
Type url http://localhost:8000/posts
in browser to test this application.