onlinecode

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

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.

  1. <?php
  2. namespace AppHttpControllers;
  3. use IlluminateHttpRequest;
  4. class MemberController extends Controller
  5. {
  6.     public functionindex(Request $request){        
  7.         $members=DB::table("members")->paginate(5);
  8. returnview('members',compact('members'))
  9.         ->with('i',($request->input('page',1)-1)*5);        
  10.     }
  11.     public functiondestroy($id){
  12.         DB::table("members")->delete($id);
  13.         returnredirect()->back()->with('success','Member deleted');
  14.     }
  15. }
<?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

  1. <!DOCTYPEhtml>
  2. <html>
  3. <head>
  4.     <title></title>
  5.     <linkrel="stylesheet"type="text/css"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  6.     <scriptsrc="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  7.     <scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8.     <scriptsrc="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. <divclass="alert alert-success">
  13. <p>{{ $message }}</p>
  14. </div>
  15. @endif
  16. <tableclass="table table-bordered">
  17. <tr>
  18. <th>No</th>
  19. <th>Name</th>
  20. <th>Email</th>
  21. <thwidth="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. <scripttype="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>
<!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


Show Demo

Label :

PHP

Laravel PHP Framework

jQuery

How To

MVC

Web Development

Bootstrap

JavaScript

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

For More Info See :: laravel And github

Exit mobile version