Laravel 5 - confirmation box for delete a member from database example

Laravel 5 - confirmation box for delete a member from database example

Laravel 5 - confirmation box for delete a member from database example

In this tutorial, I will tell you how to show confirmation prompt before deleting any thing from database in Laravel using "Bootstrap Confirmation" plugin.

This is very important to give alerts to users on each activity that may affect your database.

This example will show you that how to delete a record from MySQL Database in Laravel but before deleting a record, you will get a bootstrap confirmation box. If you confirm by clicking the delete button in confirmation box then it will delete the record from database.

Boostrap confirmation plugin provide pretty confirmation box that will not interrupt a user's workflow.

There are also lots of plugins available to show confirmatin box like sweetalert, bootbox, javascript confirm dialog etc.

Create Table "members"

To perform database activity, create a "members" table by running following query :

CREATE TABLE `members` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(100) NOT NULL,
 `email` varchar(100) NOT NULL,
 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 `updated_at` datetime NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
Add routes

To start with Laravel application, we will define routes to handle the request. In this example, I need two routes, one for listing members having delete action and another for delete activity.

So add following routes in your routes/web.php file.

routes/web.php
Route::get('members', 'MemberController@index');
Route::delete('member/{id}', ['as'=>'members.destroy','uses'=>'MemberController@destroy']);
Create MemberController

Now i will create "MemberController.php" file in following path app/Http/Controllers/MemberController.php having two methods.

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. class MemberController extends Controller
  5. {
  6.     public function index(Request $request){        
  7.         $members = \DB::table("members")->paginate(5);
  8. return view('members',compact('members'))
  9.         ->with('i', ($request->input('page', 1) - 1) * 5);        
  10.     }
  11.     public function destroy($id){
  12.         \DB::table("members")->delete($id);
  13.         return redirect()->back()->with('success','Member deleted');
  14.     }
  15. }
Create members blade file

In this step, i will create a view file "members.blade.php" in following path resources/views/members.blade.php.

To use bootstrap confirmation plugin, we need to install following dependencies :

  • bootstrap.min.css >= 3.2
  • jQuery >= 1.9
  • bootstrap.min.js >= 3.2
  • bootstrap-confirmation.min.js
resources/views/members.blade.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title></title>
  5.     <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  6.     <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  7.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8.     <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-confirmation/1.0.5/bootstrap-confirmation.min.js"></script>
  9. </head>
  10. <body>
  11. @if ($message = Session::get('success'))
  12. <div class="alert alert-success">
  13. <p>{{ $message }}</p>
  14. </div>
  15. @endif
  16. <table class="table table-bordered">
  17. <tr>
  18. <th>No</th>
  19. <th>Name</th>
  20. <th>Email</th>
  21. <th width="280px">Action</th>
  22. </tr>
  23. @foreach ($members as $member)
  24. <tr>
  25. <td>{{ ++$i }}</td>
  26. <td>{{ $member->name}}</td>
  27. <td>{{ $member->email}}</td>
  28. <td>
  29. {!! Form::open(['method' => 'DELETE','route' => ['members.destroy', $member->id],'style'=>'display:inline']) !!}
  30. {!! Form::button('Delete', ['class' => 'btn btn-danger','data-toggle'=>'confirmation']) !!}
  31. {!! Form::close() !!}
  32. </td>
  33. </tr>
  34. @endforeach
  35. </table>
  36. {!! $members->render() !!}
  37. <script type="text/javascript">
  38. $(document).ready(function () {     
  39. $('[data-toggle=confirmation]').confirmation({
  40. rootSelector: '[data-toggle=confirmation]',
  41. onConfirm: function (event, element) {
  42.     element.closest('form').submit();
  43. }
  44. });
  45. });
  46. </script>
  47. </body>
  48. </html>

You can also change the direction of confirmation box using data-placement attribute. There are four direction left, top, bottom, right.

This is much pretty in comparison with other plugins, if you wish you can use javascript confirm dialog.

If you want to know the complete CRUD functionality then click here : Laravel CRUD example

If you get "Class Form not found" error then follow the url : Click here

Phone: (+91) 8800417876
Noida, 201301