Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DDSameera's avatar

Laravel route's UPDATE method is not working (AJAX environment)

Could you please help me to resolve this issue ?

Route::resource('maincat', MainCategoryController::class);
 $('#mainCatAddEditForm').ajaxSubmit({
            url: {{url('maincat/update')}}/"+id;
            type: 'POST',
            data: {
                "name": name,
                "description": description,
                "_token": "{{ csrf_token() }}",
                "_method": "PUT"

      },
Request URL: http://localhost:8083/lms/public/maincat/update/534
Request Method: POST
Status Code: 404 Not Found
0 likes
4 replies
Niush's avatar
Niush
Best Answer
Level 50

You need to take another look at this: https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller

The URL is Restful i.e. we do not need product/update/1 it should be product/1 with method PUT/PATCH and it will trigger the update function. Similarly, product/1 with DELETE will trigger destroy.

 $('#mainCatAddEditForm').ajaxSubmit({
      url: "{{url('maincat')}}/"+id;
      type: 'POST',
      data: {
          "name": name,
          "description": description,
          "_token": "{{ csrf_token() }}",
          "_method": "PUT"
      }
})
1 like
DDSameera's avatar

@niush , I can see this error

 var id = $("#id").val();
        var name = $("#mainCatName").val();
        var description = $("#mainCatDescription").val();
        var url = "";
        var methodType = "";

        if (!id) {
            url = "{{url('maincat')}}";//Save Url
            methodType = "POST";

        } else {
            url = "{{url('maincat')}}/" + id;//Edit Url
            methodType = "PUT";
        }


        $('#mainCatAddEditForm').ajaxSubmit({
            url: url,
            type: methodType,
            data: {
                "name": name,
                "description": description,
                "_token": "{{ csrf_token() }}",
                "_method": methodType

            },
{message: "CSRF token mismatch.", exception: "Symfony\Component\HttpKernel\Exception\HttpException",…}
exception: "Symfony\Component\HttpKernel\Exception\HttpException"
file: "C:\wamp64\www\lms\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php"
line: 369
message: "CSRF token mismatch."
trace: [,…]

Please or to participate in this conversation.