I am working on a post and comment system. I created comment and Post controllers and models. When a user is logged in and comments on a post, upon deleting or editing the comment, the post is edited. Same to if the logged in user is an admin.
//Comment Controller
public function edit(Comment $comment)
{
if (auth()->user()->is_admin !== 1) {
if ($comment->user_id !== Auth::id()) {
abort(403);
}
}
return view('comments.edit', ['comment' => $comment]);
}
public function destroy(Comment $comment)
{
if (auth()->user()->is_admin !== 1) {
if ($comment->user_id !== Auth::id()) {
abort(403);
}
}
// dd($comment);
$comment->delete();
return redirect('posts/' . $comment->post->slug)->with('success', 'Comment deleted successfuly');
}
public function store(Request $request, `Comment $comment, $slug)
{
$request->validate([
'body' => 'required'
]);
$post_id = Post::where('slug', $slug)->pluck('id');
// dd($post_id[0]);
$comment->create([
'user_id' => $request->user()->id,
'post_id' => $post_id[0],
'body' => $request->input('body'),
]);
return redirect()->back();
}
// update
public function update( Comment $comment,Post $post)
{
$comment->update([
'body' => request()->body
]);
return redirect('posts/' . $comment->post->slug)-
>with('success', 'Comment updated successfuly');
}
//form
<x-panel >
<form action="{{route('comments.store', $post)}}" method="POST">
@csrf
<div class="mt-6">
<textarea name="body" cols="30" rows="" class="rounded w-
full text-sm overflow-y-scroll" placeholder="Comment as {{auth()-
>user()->name}}" required></textarea>
</div>
<div class="flex justify-end border-gray-200 pt-3">
<button type="submit" class="bg-gray-600 text-white
uppercase font-semibold text-md px-10 rounded-2xl hover:bg-gray-
800 hover:text-white">Post</button>
</div>
</form>
</x-panel>
//Routes
Route::get('/posts',[PostController::class,'index'])->name('posts.index');
Route::get('/myposts',[PostController::class,'indexx'])->name('profileposts');
Route::get('/posts/{post:slug}', [PostController::class, 'show'])->name('posts.show');
Route::get('/posts/create/post', [PostController::class, 'create'])->name('posts.create'); //shows create post form
Route::post('/posts/create/post', [PostController::class, 'store'])->name('posts.store'); //saves the created post to the database
Route::get('/posts/{post:slug}/edit', [PostController::class, 'edit'])->name('posts.edit'); //shows edit post form
Route::put('/posts/{post:slug}/edit', [PostController::class, 'update'])->name('posts.update'); //commits edited post to the database
Route::delete('/posts/{post}/', [PostController::class, 'destroy'])->name('posts.destroy'); //deletes post from the database
Route::post('/posts/{post:slug}/comments', [CommentController::class, 'store'])->name('comments.store'); //store comment
Route::get('posts/comments/{comment:slug}/edit', [CommentController::class, 'edit'])->name('comments.edit'); //edit comment
Route::put('posts/comments/{comment:slug}/edit', [CommentController::class, 'update'])->name('comments.update'); //commits edited comment to the database
Route::delete('posts/comments/{comment:slug}', [CommentController::class, 'destroy'])->name('comments.destroy');
});
- What could be the issue?
- Anyone to guide me on how to nest(add) replies.
That once I edit a comment, it is the comment that is edited, not a post.`
2
Answers
Your for have mistake in route:
you should use this route:
and also it´s recommended use add in form:
all your form shold be:
and i´ll change this route:
to:
Based on the information provided, it seems that there might be a conflict in your route definitions. The problem appears to be that the route parameter for the
update,
edit,
anddestroy
methods in the CommentController is{comment:slug}.
This parameter is also used in the PostController routes for theshow,
edit,
update,
anddestroy
methods, but as{post:slug}.
This could lead to Laravel treating the comment’s slug as if it were a post’s slug. To avoid this, I suggest changing your CommentController routes to use a different parameter, such as
{comment:id}.
Your updated routes might look like:
Your CommentController’s methods would then look like:
With these changes, Laravel should be able to differentiate between comments and posts when accessing the
edit,
update,
anddestroy
routes for comments.Lastly, ensure that in your views, you’re using the correct route and passing the correct data when editing, updating, or deleting a comment. When you use
route('comments.edit', $comment->id),
Laravel should be able to find the right route and use the correct method from your CommentController.