【问题标题】:Multiple delete in LaravelLaravel 中的多次删除
【发布时间】:2017-07-29 19:26:48
【问题描述】:

我目前正在尝试在 Laravel 5.3 中进行多次删除。我想成为像使用复选框删除电子邮件一样的东西。

但在删除之前,我想使用模态进行确认。

我的刀片中的表格看起来像这样

<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();
       });
    }
});    

如何在不为多次删除创建另一个函数和路由的情况下将我的数组提交给控制器,而只使用 links.destroy?

【问题讨论】:

    标签: laravel laravel-5.3


    【解决方案1】:

    您可以随时通过JS收集链接并发送它们。在 Laravel 控制器中,您只需添加一个 if 条件来区分这两个请求。

    顺便说一句,无论是只有一个 id 还是多个,您都应该始终确保对它们执行权限。

    详细说明我的答案,

    假设您有一个文章集合,每个文章都可以标记为删除

    <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">
    

    然后你可以收集这些 id

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

    稍后通过XMLHttpRequest 发送给他们

    【讨论】:

    • 怎么样?我对 js 了解不多
    • @GabrielLuna 如果您将复选框放在表单中,那么 Eddy 的回答实际上是最简单、最简洁的方法。
    • 是的,我正在将我的复选框放在一个表单中,我目前正在尝试 eddy 的代码.. 但仍然没有运气.. 无论如何,将探索你和 eddy 的解决方案..
    【解决方案2】:

    你可以像这样从表单中传递数组

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

    在你的控制器中你可以这样做

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

    【讨论】:

    • 我想你的意思是$request-&gt;ids
    • 确实是我的朋友。谢谢
    • 如何将我的数组传递给我的控制器?我对js不是很熟悉。
    • 我只是使用一个简单的表格。这样就可以了。 JS 只对模态进行干预。因此,您实际上应该从模态提交表单。但这与您在这里提出的问题完全不同。我认为有一些方法可以在模式打开时防止默认。并在模式关闭时提交表单。请点击此链接,他们确切地展示了如何做到这一点。 stackoverflow.com/questions/32666100/…
    • @EddyTheDove 我现在正在尝试在我的项目中实现您的代码,但模式没有出现,并且出现 $(...).validate 不是函数的错误..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2016-06-10
    • 2016-05-19
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多