phplaravellaravel-8

can not make the post request in my laravel project


I am having an issue with making a post request in my project, like whenever i am trying to make a request using a button in my application i am having an error with tp://localhost/delete-comment 404 (Not Found) send @

This is my code in which i am making the request:

<script>

        $(document).ready(function(){

        // CSRF Token
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });


            $(document).on('click', '.deleteComment', function () {


                 if(confirm('Are you sure you want to delete this comment?'))
                 {

                         var thisClicked = $(this);
                         var comment_id = thisClicked.val();
                         $.ajax({
                            type:'POST',
                            url: '/delete-comment',
                            data: {
                                'comment_id': comment_id,
                                '_token': $('meta[name="csrf-token"]').attr('content')
                            },
                            success:function(res){
                                if(res.status == 200){
                                    thisClicked.closest('.comment-container').remove();
                                    alert(res.message);
                                }
                                else{
                                    alert(res.message);
                                }
                            }
                         });


                }
            });
        });
    </script>

And this is my web.php file:

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::get('/', [App\Http\Controllers\Frontend\FrontendController::class, 'index']);
Route::get('tutorial/{category_slug}', [App\Http\Controllers\Frontend\FrontendController::class, 'viewCategoryPost']);
Route::get('/tutorial/{category_slug}/{post_slug}', [App\Http\Controllers\Frontend\FrontendController::class, 'viewPost']);

// Comment System

Route::post('comments', [App\Http\Controllers\Frontend\CommentController::class, 'store']);
Route::post('delete-comment', [App\Http\Controllers\Frontend\CommentController::class, 'destroy']); //here is the request i am getting

and below is my commentController.php file which have the destroy function:

<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Comment;
use Illuminate\Support\Facades\Validator;


class CommentController extends Controller
{
    public function store(Request $request)
    {
        if (Auth::check()) {

            $validator = Validator::make($request->all(), [
                'comment_body' => 'required|string'
            ]);
            if ($validator->fails()) {
                return redirect()->back()->with('message', 'Comment Area is Mandetory');
            }

            $post = Post::where('slug', $request->post_slug)->where('status', '0')->first();
            if ($post) {
                Comment::create([
                    'post_id' => $post->id,
                    'user_id' => Auth::user()->id,
                    'comment_body' => $request->comment_body,
                ]);
                return redirect()->back()->with('message', 'Commented Successfully');
            } else {
                return redirect()->back()->with('message', 'No Such Post Found!');
            }
        } else {
            return redirect('login')->with('message', 'Login first to comment');
        }
    }


    public function destroy(Request $request) //here is the destroy function
    {
        if (Auth::check()) {
            $comment = Comment::where('id', $request->comment_id)->where('user_id', Auth::user()->id)->first();

            if ($comment) {
                $comment->delete();
                return response()->json([
                    'status' => 200,
                    'message' => 'Comment Deleted Successfully',
                ]);
            } else {

                return response()->json([
                    'status' => 500,
                    'message' => 'Something Went Wrong',
                ]);
            }
        } else {
            return response()->json([
                'status' => 401,
                'message' => 'Login to Delete this Comment'
            ]);
        }
    }
}


Solution

  • To make sure you put the correct url in ajax. First, name your route:

    Route::post('delete-comment', [App\Http\Controllers\Frontend\CommentController::class, 'destroy'])->name('deleteComment');
    

    Then:

    $.ajax({
       type:'POST',
       url: '{{route('deleteComment')}}',