How to delete record in Laravel with ajax
How to delete record in Laravel with ajax
We’ll attempt to use programming in this lesson to solve the "How to delete record in Laravel with ajax" puzzle.
You can delete the record in Laravel with ajax. You have to pass the primary id of the record as the route parameter and call the ajax script to delete the record from the table.-
Delete a record with Ajax using HTML Form in Laravel
//Web route to delete record from users table routes\web.php Route::delete('/user/delete/{id}', 'UserController@delete')->name('user.delete'); //HTML - Delete user form with ajax script //resources\views\home.blade.php <form class="delete-user-form" action="{{ route('user.delete', 1) }}" method="POST"> {{ csrf_field() }} {{ method_field('DELETE') }} <button type="submit" class="btn btn-default">Remove</button> </form> <script> $(document).on('submit', '.delete-user-form', function(e) { e.preventDefault(); var url = $(this).attr('action'); $.ajax({ type: "DELETE", url: url, data:{ _token: '{{ csrf_token() }}' }, success: function(resp){ console.log(resp); } }); return false; }); </script> //UserController’s delete method app\Http\Controllers\UserController.php public function delete($id){ $resp = array(); try{ User::where('id', $id)->delete(); $resp['status'] = true; }catch(\Exception $e){ $resp['status'] = false; $resp['error_msg'] = $e->getMessage(); } return response()->json($resp); }
You can delete record in laravel using ajax. You have to create a web route and add html form with ajax script in blade file. You have to create a delete method in controller and add delete functionality to delete the record from database table.
Additional Note:
Add jQuery script in Head or body tag in Laravel blade file.
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Call to undefined method App\Models\User::follow()
- Laravel form request validation
- How to use bootstrap pagination in laravel 8
- Json encode method in laravel
- How to get all posts which contains comments in laravel
- External link not working in laravel blade
- Get current month records in laravel 7/8
- Get products with number of orders in Laravel
- Laravel 7 login error message not showing
- How to create pivot table in laravel using migration
- How to call model in blade laravel
- How to check column value of a record is null or not in laravel
- Laravel URL validation not working
- How to get images from AWS s3 and display in Laravel blade
- How to validate URL with https using regex in laravel
- Call to undefined relationship [user] on model [App\Models\Post]
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- FirstOrCreate() Not Inserting Model
- How to send email in Laravel 11
- Best Practices for Error Handling in Production Server Code (example code)
- Post model with title and body in laravel 8
- How to get count of all records created at yesterday
- How to get column names from table in Laravel
- How to get only time from created_at in laravel