How to delete record in laravel 5.3 using ajax request?

laravel ajax delete method not allowed
laravel 6 ajax delete
how to delete image using ajax in laravel
insert update delete using ajax in laravel
change status using ajax in laravel
delete with confirmation in laravel ajax
ajax delete request
ajax delete request with parameters

I'm trying to delete record using ajax in laravel 5.3, i know this is one of the common question and there is already lots of online solutions and tutorials available about this topic. I tried some of them but most of giving me same error NetworkError: 405 Method Not Allowed. I tried to do this task by different angle but i'm stuck and could not found where i'm wrong, that's why i added this question for guideline.

I'm trying following script for deleting the record.

Controller.php

public function destroy($id)
{   //For Deleting Users
    $Users = new UserModel;
    $Users = UserModel::find($id);
    $Users->delete($id);
    return response()->json([
        'success' => 'Record has been deleted successfully!'
    ]);
}

Routes.php

Route::get('/user/delete/{id}', 'UserController@destroy');

In View

<button class="deleteProduct" data-id="{{ $user->id }}" data-token="{{ csrf_token() }}" >Delete Task</button>

App.js

$(".deleteProduct").click(function(){
        var id = $(this).data("id");
        var token = $(this).data("token");
        $.ajax(
        {
            url: "user/delete/"+id,
            type: 'PUT',
            dataType: "JSON",
            data: {
                "id": id,
                "_method": 'DELETE',
                "_token": token,
            },
            success: function ()
            {
                console.log("it Work");
            }
        });

        console.log("It failed");
    });

When i'm click on delete button it returning me error NetworkError: 405 Method Not Allowed in console. Without ajax same delete function is working properly.

Can anyone guide me where i'm wrong that i can fix the issue, i would like to appreciate if someone guide me regarding this. Thank You..

Instead of using Route::get use Route::delete.

In addition to that change the type: 'Put' to type: 'DELETE' in the ajax call.


P.S. This code

$Users = new UserModel;        // Totally useless line
$Users = UserModel::find($id); // Can chain this line with the next one
$Users->delete($id);

can be written as:

UserModel::find($id)->delete();

Or even shorter:

UserModel::destroy($id);

Keep in mind that ->delete() will fire an event while ::destroy() will not.

Insert Update and Delete record with AJAX in Laravel, we will create delete route with controller method(we will write delete row code using database model) and write jquery ajax code with delete post request. we also  A very few days ago, i was trying to delete record using jquery ajax request in my laravel 5.7 app. i always make delete record using jquery ajax, so i also want to delete record with ajax request in laravel 5.7.

Make sure to add this in the meta tag of your view

    <meta name="csrf-token" content="{{ csrf_token() }}">

In your Routes, do this

Route::delete('/user/delete/{id}', 'UserController@destroy');

In your controller, do this

UserModel::destroy($id);

or

DB::table('table_name')->where('id', $id)->delete();

Make sure to check that the user who is deleting the account actually owns the account a.k.a run authorization test.

Since it's a delete request, you would require to send the csrf_token along with your ajax header as the official site states. https://laravel.com/docs/5.5/csrf#csrf-x-csrf-token

Make sure to add this before the ajax call

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

Now send the request

$(".deleteProduct").click(function(){
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax(
    {
        url: "user/delete/"+id,
        type: 'delete', // replaced from put
        dataType: "JSON",
        data: {
            "id": id // method and token not needed in data
        },
        success: function (response)
        {
            console.log(response); // see the reponse sent
        },
        error: function(xhr) {
         console.log(xhr.responseText); // this line will save you tons of hours while debugging
        // do something here because of error
       }
    });
});

I hope this helps.

Delete record using ajax request in Laravel Example, Issue while delete record in laravel 5.4 using ajax request? I want to delete a record using ajax call with laravel 5.4. I tried a lot but delete is is not working means  Today i will show you how to delete data ajax request in Laravel. you have to simply follow few things to make done delete record from database using ajax request.we will create delete route with controller method(we will write delete row code using database model) and write jquery ajax code with delete post request. we also pass csrf token in jquery ajax request, otherwise it will return error like delete method not allowed.

$(".deleteProduct").click(function(){
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
$.ajax(
{
    url: "user/delete/"+id,
    type: 'DELETE', // Just delete Latter Capital Is Working Fine
    dataType: "JSON",
    data: {
        "id": id // method and token not needed in data
    },
    success: function (response)
    {
        console.log(response); // see the reponse sent
    },
    error: function(xhr) {
     console.log(xhr.responseText); // this line will save you tons of hours while debugging
    // do something here because of error
   }
});

});

Delete record using ajax request in Laravel Example, we will create delete route with controller method(we will write delete row code using database model) and write jquery ajax code with delete post  We will use POST request to delete data from database using ajax request. we will create delete route with controller method (we will write delete row code using database model) and write jquery ajax code with delete post request. we also pass csrf token in jquery ajax request, otherwise it will return error like delete method not allowed.

i'm resuming a working flow of deletion, with a request VERB. Hope it helps

and theres a commented code in the controller that could handle an ajax request

In the form (with blade):

  {{ Form::open(['method' => 'DELETE', 'route' => ['admin.products.edit', $product->id], 'name' => 'delete']) }}
    {{ Form::close() }}

Route:

Route::delete('admin/products/{id}/edit', ['as' => 'admin.products.edit', 'uses' => 'Product\ProductController@delete']);

ProductController:

 public function delete($id)
    {
        // if (Request::ajax()) {
        // if (Request::isMethod('delete')){

        $item = Product::findOrFail($id);
        $item->delete();

        return redirect()->route('admin.products')->with('flashSuccess', 'deleted');
    }

In the redirect part, i'm going back to my list page (admin.products) with a success notifier. The route would be:

Route::get('admin/products', ['as' => 'admin.products', 'uses' => 'Product\ProductController@getList']);

So you can complete the flow.

Deleting a record in Laravel 5.3 using AJAX, I have table with rows and their id, in view smth like this: <body> <table> <tbody> <tr> <td> User id: {{ $user->id }} </td> <td> <button class="btn  How to delete data from database - Laravel and Ajax - Learn how to delete data in database using Laravel and Ajax complete source code and demo.

Delete Record using Ajax Request in Laravel, We can delete the record using the DB facade with the delete method. The syntax of delete method is shown in the following table. Syntax, int delete(string $query,​  There are the Following The simple About Laravel AJAX Delete Record From MySQL Table Full Information With Example and source code.. As I will cover this Post with live Working example to develop Deleting item using Ajax request with DELETE and OPTIONS, so the Delete record using ajax request in Laravel Example is following below.

Why my DELETE Ajax Request in laravel 5.3 does not working , In this example, you will learn how to delete all records or multiple records on a single click by selecting checkbox using jQuery Ajax request in Laravel PHP. Insert Update and Delete record with AJAX in Laravel AJAX is used to communicate with the server and perform actions like – database manipulation, file upload, etc. without the need to refresh the whole page.

Laravel - Delete Records, laravel crud modal,laravel5 crud ajax tutorial,laravel crud admin,crud i had done CRUD application in Laravel MVC without ajax request, now i am going to tell you how to build CRUD web application without page refresh in Laravel using ajax. $.ajax({; type: "DELETE",; url: url + '/' + product_id,; success: function (data​)  I will give you very simple example of laravel 6 ajax post request tutorial. you can easily use ajax get request, ajax post request, ajax put request, ajax delete request ect with laravel 6. Ajax request is a basic requirement of any php project, we are always looking for without page refresh data should store in database and it's possible only

Comments
  • Thanks for guideline, i followed your instructions but still facing same issue.
  • Have you tried using type: 'DELETE' in your ajax call?
  • Let me check it
  • Wow Perfect, Now its working. Thanks a lot for guideline +1