Laravel 5 – confirmation box for delete a member from database example
In this post we will give you information about Laravel 5 – confirmation box for delete a member from database example. Hear we will give you detail about Laravel 5 – confirmation box for delete a member from database exampleAnd how to use it also give you demo for it if it is necessary.
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.
- <?php
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- class MemberController extends Controller
- {
- public functionindex(Request $request){
- $members=DB::table("members")->paginate(5);
- returnview('members',compact('members'))
- ->with('i',($request->input('page',1)-1)*5);
- }
- public functiondestroy($id){
- DB::table("members")->delete($id);
- returnredirect()->back()->with('success','Member deleted');
- }
- }
<?php namespace AppHttpControllers; use IlluminateHttpRequest; class MemberController extends Controller { public function index(Request $request){ $members = DB::table("members")->paginate(5); return view('members',compact('members')) ->with('i', ($request->input('page', 1) - 1) * 5); } public function destroy($id){ DB::table("members")->delete($id); return redirect()->back()->with('success','Member deleted'); } }
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
- <!DOCTYPEhtml>
- <html>
- <head>
- <title></title>
- <linkrel="stylesheet"type="text/css"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <scriptsrc="https://code.jquery.com/jquery-3.2.1.min.js"></script>
- <scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- <scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-confirmation/1.0.5/bootstrap-confirmation.min.js"></script>
- </head>
- <body>
- @if ($message = Session::get('success'))
- <divclass="alert alert-success">
- <p>{{ $message }}</p>
- </div>
- @endif
- <tableclass="table table-bordered">
- <tr>
- <th>No</th>
- <th>Name</th>
- <th>Email</th>
- <thwidth="280px">Action</th>
- </tr>
- @foreach ($members as $member)
- <tr>
- <td>{{ ++$i }}</td>
- <td>{{ $member->name}}</td>
- <td>{{ $member->email}}</td>
- <td>
- {!! Form::open(['method' => 'DELETE','route' => ['members.destroy', $member->id],'style'=>'display:inline']) !!}
- {!! Form::button('Delete', ['class' => 'btn btn-danger','data-toggle'=>'confirmation']) !!}
- {!! Form::close() !!}
- </td>
- </tr>
- @endforeach
- </table>
- {!! $members->render() !!}
- <scripttype="text/javascript">
- $(document).ready(function () {
- $('[data-toggle=confirmation]').confirmation({
- rootSelector: '[data-toggle=confirmation]',
- onConfirm: function (event, element) {
- element.closest('form').submit();
- }
- });
- });
- </script>
- </body>
- </html>
<!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-confirmation/1.0.5/bootstrap-confirmation.min.js"></script> </head> <body> @if ($message = Session::get('success')) <div > <p>{{ $message }}</p> </div> @endif <table > <tr> <th>No</th> <th>Name</th> <th>Email</th> <th width="280px">Action</th> </tr> @foreach ($members as $member) <tr> <td>{{ ++$i }}</td> <td>{{ $member->name}}</td> <td>{{ $member->email}}</td> <td> {!! Form::open(['method' => 'DELETE','route' => ['members.destroy', $member->id],'style'=>'display:inline']) !!} {!! Form::button('Delete', ['class' => 'btn btn-danger','data-toggle'=>'confirmation']) !!} {!! Form::close() !!} </td> </tr> @endforeach </table> {!! $members->render() !!} <script type="text/javascript"> $(document).ready(function () { $('[data-toggle=confirmation]').confirmation({ rootSelector: '[data-toggle=confirmation]', onConfirm: function (event, element) { element.closest('form').submit(); } }); }); </script> </body> </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
Hope this code and post will helped you for implement Laravel 5 – confirmation box for delete a member from database 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