Delete record using ajax request in Laravel Example

Delete record using ajax request in Laravel Example

In this post we will give you information about Delete record using ajax request in Laravel Example. Hear we will give you detail about Delete record using ajax request in Laravel ExampleAnd how to use it also give you demo for it if it is necessary.

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.

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.

you have to simply follow few things to make done delete record from database using ajax request. Let’s follow few steps.

Create Route: routes/web.php

Route::delete('users/{id}', 'UserController@destroy')->name('users.destroy');

Controller Method: app/Http/Controllers/UserController.php

public function destroy($id){

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

return response()->json([

'success' => 'Record deleted successfully!'

]);

}

View Code: resources/views/users.php

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

<button data-id="{{ $user->id }}" >Delete Record</button>

JS Code: resources/views/users.php

Also see:Laravel 5.7 Import Export Excel to database Example

$(".deleteRecord").click(function(){

var id = $(this).data("id");

var token = $("meta[name='csrf-token']").attr("content");

$.ajax(

{

url: "users/"+id,

type: 'DELETE',

data: {

"id": id,

"_token": token,

},

success: function (){

console.log("it Works");

}

});

});

Now you can check it.

I hope it can help you…

Hope this code and post will helped you for implement Delete record using ajax request in Laravel Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

37  +    =  46

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US