Jxmccall
Jxmccall

Reputation: 35

Passing row id to laravel controller from bootstrap 3 delete modal

Learning Laravel (4.1) and I am having trouble with passing a row id from a table to a controller. What I want to do is the user click on the icon to delete the record. A Modal Window will come up first and ask if they are sure they want to delete. If they click Yes then ID should be sent to the controller so the delete can occur.

My Laravel Route looks like this

 DELETE entir/guests/{guests}   | entir.guests.destroy | GuestController@destroy 

Controller for the destroy looks like this. For now I am just printing out the ID and a message.

    public function destroy($id)
{
 print_r($id);
return 'You called the Destroy function';
}

The html code that builds the table is

<table id="table" data-toggle="table" data-url="{{ action('GuestController@show') }}" data-click-to-select="true" data-single-select="true" data-pagination="true" data-search="true">                 
<thead>
    <tr>
        <th data-field="id" data-align="right" data-sortable="true">ID</th>
        ...
        <th data-field="operate" data-formatter="operateFormatter" data-events="operateEvents">Actions</th>
    </tr>
</thead>

Here is the javascript for the table and delete event.

<script>
function operateFormatter(value, row, index) {
    return [
        '<a class="edit ml10" href="javascript:void(0)" title="Entir Guest">',
        '<i class="glyphicon glyphicon-edit"></i>',
        '</a>',
        '<a class="remove ml10" href="javascript:void(0)" title="Remove">',
        '<i class="glyphicon glyphicon-remove"></i>',
        '</a>'
        ].join('');
}
window.operateEvents = {                                
        'click .remove': 
        function (e, value, row, index) {
            $('#deleteModal').data('id', row.id).modal('show');
                var id = JSON.stringify(row.id);
                // Alert the ID for testing                                 
                alert('Remove ID: ' + id);                
            }
};

Delete Modal html

<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header modal-header-danger">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">Warning!</h4>
        </div>
        <div class="modal-body">
            <h4> Are you sure you want to Delete?</h4>
        </div>
        <!--/modal-body-collapse -->
        <div class="modal-footer">
            {{Form::open(array('method'=>'DELETE', 'route' => array('entir.guests.destroy')))}}
            <button type="submit" class="btn btn-danger" id="btnDelteYes">Yes</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
            {{ Form::close() }}
        </div>
        <!--/modal-footer-collapse -->
    </div>
    <!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->

Lets say I have rows 1 - 20 with ID's 1 -20. If I choose Row 1 and click the delete icon, I get an alert that says "Remove ID: 1". Then the Modal window comes up and asks you to confirm. If I press yes the controller is called as I get the message from the destroy function. As you can see I am not passing the ID at this point as I am unsure of the exact way to do this. I have tried a few examples I have found online but I end up getting a variable not defined error in laravel. Any help is appreciated.

This is where I am assuming I need to pass the ID

{{Form::open(array('method'=>'DELETE', 'route' => array('entir.guests.destroy')))}}

Upvotes: 0

Views: 1359

Answers (1)

Jxmccall
Jxmccall

Reputation: 35

Solved the issue by passing the ID via the controller as a href.

Upvotes: 1

Related Questions