PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] Why does delete request return a 404 in Laravel using ajax

 April 19, 2022     ajax, delete-row, laravel, php     No comments   

Issue

This is the route inside my web.php file It says 404 Not found on the route http://127.0.0.1:8000/categories/delete

Route::middleware(["auth"])->group(function () {
    Route::resources([
        'categories' => CategoryController::class,
        'posts' => PostsController::class,
    ]);
    
    // this is the route i am targeting
    Route::delete("/categories/delete", [CategoryController::class, "delete"])->name("categories.delete"); 
});

This is the ajax request to the route inside my index.blade.php file

<button id="deleteAll" class="border-0" type="button">
    <x-heroicon-o-trash class="w-6 h-6 text-red-600" />
</button>

<script>
    $(function(){
        $("#check-all").click(function(){
            $(".item-check").prop("checked", $(this).prop('checked'));
        });

        // This is the click event to delete a category
        $("#deleteAll").click(function(e){
            e.preventDefault();

            let allIds = [];
            $("input:checkbox[name=catId]:checked").each(function(){
                allIds.push($(this).val());
            });

            $.ajax({
                url: "{{ route('categories.delete') }}",
                type: "DELETE",
                data: {
                    _token: $("input[name=_token]").val(),
                    ids: allIds
                },
                success: function(response){
                    $.each(ids, function(key, val){
                        $("#row-"+val).remove(); 
                    })
                }
            });
        });
    });
</script>

Here is the delete function within my CategoryController

public function delete(Request $request)
{
   dd($request->all());
}

Solution

In think you must change your routes order:

your web.php file could be like this:

Route::middleware(["auth"])->group(function () {

    // this is the route i am targeting
    Route::delete("/categories/delete", [CategoryController::class, "delete"])->name("categories.delete"); 

    Route::resources([
        'categories' => CategoryController::class,
        'posts' => PostsController::class,
    ]);
    
});

If you want to add custom route to resource routing, you must use custom ones before resource route. for more information go to resource.



Answered By - Hamid Moladoust
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing