Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bigbear's avatar

Laravel confirm delete in an alert in my view

This should be a simple task I am just not fully grasping laravel yet. I have my controllers view and models setup. I want to use my users.destroy route to delete my row in the db. But I want to do it a certain way. I want to have an alert show In my alert area on my page asking to confirm the deletion of a certain user. Im assuming I need to pass the user id in a session to an alert to confirm my delete on a delete button click. Click 1 button to open an alert on the top of my page if I click confirm it calls user.destroy.

<div class="row justify-content-center">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h4>View All Users</h4>

                @if(session()->get('success'))
                    <div class="alert alert-success">
                        {{ session()->get('success') }}
                    </div>
                @endif
                @if(session()->get('danger'))
                    <div class="alert alert-danger">
                        {{ session()->get('danger') }}
                    </div>
                @endif
            </div>
            <div class="card-body">
                <div class="text-center my-2">
                    <a href="{{ route('register') }}" class="btn btn-primary">New User</a>
                </div>
                <div>
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Username</th>
                                <th colspan="2">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($users as $user)
                            <tr>
                                <th>{{$user->id}}</th>
                                <td>{{$user->name}}</td>
                                <td>{{$user->email}}</td>
                                <td>{{$user->username}}</td>
                                <td class="text-center">
                                    <a href="{{ route('users.show', $user->id) }}" class="btn btn-primary mr-3">Show</a>
                                    <a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white ml-3">Edit</a>
                                    <a href="#" class="btn btn-danger">Delete</a>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
public function destroy($id)
{
    User::find($id)->delete();
    return redirect()->route('users.index')->with('success','User Deleted');
}
Route::resource('users', 'UserController');```
0 likes
8 replies
Tangente's avatar

Something like this...:


<a class="btn btn-danger" onclick="return confirm('Are you sure?')" href="{{route('users.delete', $user->id)}}"></a>

2 likes
bigbear's avatar

I was thinking of something that simple but I want the box to pop up on the top of my page like a session alert and to confirm that for the deletion

STEREOH's avatar

You apparently are using Bootstrap , just make a modal. You'll find it in bootstrap Doc. (well you can make a modal even without a framework just pointing how easy it is since you have it)

munazzil's avatar

Use this form for your edit,delete and show.

<form action="{{ route('user.destroy',$user->id) }}" method="POST" >
    <a href="{{ route('users.show', $user->id) }}" class="btn btn-primary mr-3">Show</a>
            <a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white ml-3">Edit</a>

                @csrf
                @method('DELETE')

 <button type="submit" class="btn btn-danger" onclick="return confirm('Sure Want Delete?')">Delete</button>
 </form>
bigbear's avatar

I have a modal How can I use my $user->id in my modal and put it in a button 'yes' to call my route destroy

<div class="modal" id="mdelete" role="dialog" aria-labelledby="moddelete">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title" id="moddelete">Confirm Delete</h5>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">

                                    <p>Are you sure you want to delete</p>
                                </div>
                                <div class="modal-footer">
                                    <input type="hidden" name="txtid" id="txtid" />
                                    <input type="text" name="uid" id="uid" />
                                    
                                    <button type="button" class="btn btn-danger " data-dismiss="modal">No</button>
                                
                                    <span class="text-right">
                                        <button type="button" class="btn btn-primary btndelete">Yes</button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
$(document).ready(function() {

    $('#mdelete').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        var userid = button.data('id');
        var uname = button.data('name');
        var modal = $(this);
        modal.find('#txtid').val(userid);
        modal.find('#uid').val(userid);
        modal.find('.modal-body').text(
            'Are you sure you want to delete ' + uname);
    })
});
Snapey's avatar

This is quite a neat solution http://bootstrap-confirmation.js.org/#examples

I used to code my own, but just use this on BS4 sites

    <form action="{{route('users.destroy',$user) }}" method="POST">
        @method('DELETE')
        @csrf
        <button class='btn btn-danger float-right btn-sm' type="submit" data-toggle="confirmation" ><i class="fas fa-user-times mr-2"></i>Delete User</button>
    </form>

//
//
    <script src="/js/bootstrap-confirmation.js" defer></script>
    <script>
        window.onload = function () {
            $('[data-toggle=confirmation]').confirmation({
                rootSelector: '[data-toggle=confirmation]',
            });
        }
    </script>

Cronix's avatar

I think the easiest would be to manually construct the forms action url instead of using laravels route() helper in javascript.

In the modal, create an actual form with an id, something like

<form id="delete-form" method="post">
    @csrf 
    @method('DELETE')

    <button type="button" class="btn btn-danger " data-dismiss="modal">No</button>
                                
    <span class="text-right">
        <button type="button" class="btn btn-primary btndelete">Yes</button>
    </span>
</form

Then just add the forms action attribute when you open the modal so it points to the correct url for deleting that user. You're getting the user id, so just add it to the url.

$('#mdelete').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget);
        var userid = button.data('id');
        var uname = button.data('name');
        var modal = $(this);

        modal.find('#delete-form').attr('action', '/your/url-to-delete/' + userid);

        modal.find('#txtid').val(userid);
        modal.find('#uid').val(userid);
        modal.find('.modal-body').text(
            'Are you sure you want to delete ' + uname);
    })

I think that would work.

Please or to participate in this conversation.