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
- Display success message in laravel
- How to upload local Laravel project to server ?
- How to display a specific word from a string in laravel
- Check if Relationship Method Exists in Laravel
- Insert data with form validation using ajax in laravel
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- How to print form data in laravel
- Get 30 days older records from table in laravel
- Convert multidimensional array to single array in Laravel
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- How to insert multiple rows in mysql using loop in laravel?
- Redirect from www to non www in laravel using htaccess
- Pass value from controller to model in laravel
- How to check duplicate entry in laravel
- Json encode method in laravel
- Create project table with model and migration
- How to add class to tr in table using foreach in laravel
- Get ids in array from users table
- Get all users except the followings users in overtrue laravel-follow
- Laravel 11 project setup on localhost using breeze with blade step by step
- How to check query string exists or not in laravel blade
- Get last record from table in laravel
- Remove several global scope from query
- Laravel recursive function in controller
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"