Real-World Use Case:
A Laravel application will allow comments to be posted in every user’s posts.
Create comments table using migration
php artisan make:migration create_comments_table
Edit the create_comments_table migration file
create_comments_table migration file
public function up(): void
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->integer('user_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->text('body');
$table->timestamps();
});
}
Run the migration
php artisan migrate
Create Comment Model
php artisan make:model Comment
Update the Post and Comment Model
Post Model
use HasFactory;
protected $fillable = ['title', 'body'];
public function comments()
{
return $this->hasMany(Comment::class)->latest();
}
Comment Model
use HasFactory;
protected $fillable = ['user_id', 'post_id','body'];
public function user()
{
return $this->belongsTo(User::class);
}
Create route for save comment function inside web.php
Route::post('/posts/commentsave', [App\Http\Controllers\PostController::class, 'commentSave'])->name('posts.commentsave');
Create the Comment input box
<form method="post" action="{{ route('posts.commentsave') }}" id="commentform">
@csrf
<div class="row" id="comments">
<div class="col-md-12 ml-1">
<div class="input-group input-group-sm mb-3">
<input type="text" class="form-control" name="feedComment" placeholder="Add a comment..." aria-label="Add a comment" aria-describedby="button-addon2">
<input type="hidden" name="postid" value="{{ $post->id}}" id="postid"/>
<button class="btn btn-sm btn-success" type="submit" id="button-addon2">Send</button>
</div>
</div>
</div>
</form>
In your PostController, create a function to save the comment submitted
public function commentSave(Request $request)
{
$addcomment = Comment::create([
'body' => $request->feedComment,
'post_id' => $request->postid,
'user_id' => Auth()->user()->id
]);
return back()->with('success','Comment added successfully.');
}
Update Post page to display comments after each post
<div class="row border-1 justify-content-center" id="commentdrop-{{ $post->id }}">
@foreach($comments as $comment)
@if ($comment->post_id == $post->id)
<div class="col-md-9 ml-2">
<span style="font-size:0.7rem; padding-bottom: 10px;"> {{$comment->user->name}}</span>
</div>
<div class="col-md-3 text-end">
<i class="fa fa-clock"></i><span style="font-size:0.7rem; padding-bottom: 10px;"> {{ $comment->created_at->diffForHumans() }}</span>
</div>
</div>
<div class="row">
<div class="col-md-12 ml-2">
<span style="font-size:0.8rem; padding-bottom: 10px;">{{$comment->body}}</span>
</div>
</div>
@endif
@endforeach
</div>
Update Post Controller to display comments
public function postcomment()
{
$posts = Post::get();
$comments = Comment::get();
return view('postcomment',compact('posts','comments'));
}
Full Code for the Post Page
<div class="row justify-content-center">
<div class="col-md-6">
<div class="row">
@foreach($posts as $post)
<div class="card mb-2">
<div class="card-header">{{ $post->title }}</div>
<p>{{$post->body}}</p>
<hr>
<div class="row border-1 justify-content-center" id="commentdrop-{{ $post->id }}">
@foreach($comments as $comment)
@if ($comment->post_id == $post->id)
<div class="col-md-9 ml-2">
<span style="font-size:0.7rem; padding-bottom: 10px;"> {{$comment->user->name}}</span>
</div>
<div class="col-md-3 text-end">
<i class="fa fa-clock"></i><span style="font-size:0.7rem; padding-bottom: 10px;"> {{ $comment->created_at->diffForHumans() }}</span>
</div>
<div class="row">
<div class="col-md-12 ml-2">
<span style="font-size:0.8rem; padding-bottom: 10px;">{{$comment->body}}</span>
</div>
</div>
@endif
@endforeach
</div>
<form method="post" action="{{ route('posts.commentsave') }}" id="commentform">
@csrf
<div class="row" id="comments">
<div class="col-md-12 ml-1">
<div class="input-group input-group-sm mb-3">
<input type="text" class="form-control" name="feedComment" placeholder="Add a comment..." aria-label="Add a comment" aria-describedby="button-addon2">
<input type="hidden" name="postid" value="{{ $post->id}}" id="postid"/>
<button class="btn btn-sm btn-success" type="submit" id="button-addon2">Send</button>
</div>
</div>
</div>
</form>
</div>
@endforeach
</div>
</div>
</div>
