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
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort