Gabriel Luna
Gabriel Luna

Reputation: 127

Multiple delete in Laravel

I'm currently trying to do multiple deletion in Laravel 5.3. I'd like to be something just like how emails are deleted using checkbox.

Something like this

But before deleting, I'd like to do confirmation using a modal.

Modal Confirmation

form in my blade looks like this

<form id="linkForm" action="/links/deleteLinks" method="POST">    
                {{ csrf_field() }}        
                {{ method_field('POST') }}
            <div class="ibox-content">

            <div class="table-responsive">
            <table class="table table-striped table-bordered table-hover dataTables-example footable toggle-arrow-tiny">  



                <div class="btn-group btn-group-sm pull-right">
                    <a data-toggle="modal" class="btn btn-primary" href="#modal-form-add" data-dismiss="modal"><i class="fa fa-plus-square-o"></i> Add</a>
                    <input class="btn btn-danger" type="submit" value="Delete">

                </div>

                <thead>
                <tr>
                    <th></th>
                    <th data-toggle="true" data-hide="all">Company</th>
                    {{-- <th>Category</th>     --}}
                    <th>Page Url</th>
                    <th data-hide="all">Domain Url</th>
                    <th>Destination Url</th>
                    <th>Contact Email</th>
                    <th data-hide="all">Trust Flow</th>
                    <th data-hide="all">Estimated Traffic</th>
                    <th data-hide="all">Domain Authority</th>
                    <th>Status</th>
                    <th>Note</th>
                    <th data-hide="all">Live</th>
                    <th>Action</th>

                    {{-- <th>Delete</th> --}}
                </tr>
                </thead>
                <tbody>


                    @foreach($links as $link)
                    <tr>
                        <td align="center"><input type="checkbox" class="i-checks" id="input" name="input[]"  value="{{ $link->page_url }}"></td>
                        <td>{{ $link->company->name }}</td> 
                        {{-- <td>{{ $link->category_id }}</td> --}}
                        <td>{{ $link->page_url }}</td>
                        <td>{{ $link->domain_url }}</td>
                        <td>{{ $link->destination_url }}</td>
                        <td>{{ $link->contact_email }}</td>
                        <td>{{ $link->trust_flow}}</td>
                        <td>{{ $link->estimated_traffic }}</td>
                        <td>{{ $link->domain_authority }}</td>
                        <td>{{ $link->status }}</td>
                        <td>{{ $link->comment }}</td>
                        <td>
                            @if($link->is_live)
                                {{ 'Live' }}
                            @else
                                {{ 'Down' }}
                            @endif
                        </td>
                        <td>
                            <a data-toggle="modal" href="#modal-form-{{ $link->id }}" data-dismiss="modal"><i class="fa fa-pencil"></i> Edit</a>

                        </td>



                    </tr>

                    @endforeach
                 </tbody>
            </table>

            </div>
            </div>
            </form>

modal.blade.php

<div id="myModal" class="modal inmodal fade" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">

            <div class="modal-body text-center">
                 <i class="fa fa-exclamation-triangle modal-icon"></i>
                <h2><strong>Are you sure?</strong></h2>   

                {{-- <p class="text-muted">Are you sure you want to delete the following link(s)?</p> --}}


                <p id="checkid"></p>

                <div class="row">
                    <button id="cancelDelete" type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                     <button class="btn-primary btn" id="SubForm">Confirm and Submit The Form</button>
                </div>

            </div>



        </div>
    </div>

javascript

    $("#linkForm").validate({

    submitHandler: function (form) {
        $("#myModal").modal('show');
        $('#SubForm').click(function () {
            form.submit();
       });
    }
});    

How can I submit my array to the controller without creating another function and route for multiple delete and just use links.destroy?

Upvotes: 1

Views: 1755

Answers (2)

42linoge
42linoge

Reputation: 26

You can always collect the links through JS and send them along. In the Laravel controller you'll simply add an if condition to differentiate between the two requests.

By the by, whether it is only one id or several, you should always make sure that permissions over them are enforced.

Elaborating on my answer,

Suppose you've got a collection of articles, each of which can be marked for deletion

<input type="checkbox" name="article[]" value="1" checked>
<input type="checkbox" name="article[]" value="2" checked>
<input type="checkbox" name="article[]" value="3" checked>
<input type="checkbox" name="article[]" value="4">

Then you can collect those ids with

let ids = Array.from(document.querySelectorAll('input[name="article[]"'))
               .filter(el => el.checked)
               .map(el => el.value);

And later on send them thorugh an XMLHttpRequest

Upvotes: 0

EddyTheDove
EddyTheDove

Reputation: 13259

You can pass the array from the form like this

<input type="checkbox" name="ids[]" value="{{$row->id}}"/>

In your controller you can do

Model::whereIn('id', $request->ids)->destroy();

Upvotes: 2

Related Questions