如何在laravel中以模态运行控制器函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在laravel中以模态运行控制器函数相关的知识,希望对你有一定的参考价值。

我想要做的是在模态中运行一个控制器删除函数,它在循环结束时,当我尝试使用该删除函数时,它获取最后一个id并删除它,我希望它得到当前的id在哪个函数被调用。我该怎么办?我不能把模型的代码放在CSS文件中导致它扰乱其他样式,所以脚本代码应该在刀片视图中

@section('content')
@if($promotions)
    <section class="content">
        <div class="row">
            <div class="box overflowhidden">
                <div class="box-body datatablelist">
   <table id="example1" class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>Sr No</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach ($promotions as $promotion)
                            <tr>
                                <td>
                                    @if(!empty($promotion->id))
                                        {{$promotion->id}}
                                    @else
                                        N/A
                                    @endif
                                </td>
                                <td>
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default dropdown-toggle"
                                                data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            Action <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu">

                                            <li>
     <a href="#" class="myBtn" id="myBtn"onclick="showDeleteModal(this.id)">
       <i class="fa fa-trash-o"   aria-hidden="true"></i> Delete
                                                </a>
                                            </li>
                                          </ul>
                                    </div>


                                </td>
                            </tr>
                        @endforeach
                    </table>
                </div>
                <!-- /.box-body -->
            </div>


            <!-- /.box -->
        </div>
        <!-- /.col -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->

    <!-- page script -->
    <div id="myModal" class="modal fade">
        <!-- Modal content -->
        <div class="modal-content">
            <span data-dismiss="modal" class="close">&times;</span>
            <p>Are you sure you wants to delete the promotion</p>
   {!! Form::open(['method'=>'DELETE','action'=>
    ['PromotionController@destroy',$promotion->id]]) !!}
            <div class="form-group">
  {{ Form::button('<i class="fa fa-trash-o"> Delete</i>', ['type' => 
    'submit', 'class' => 'btn btn-danger pull-right btn-
   sm','style'=>'margin-bottom:1em'] )  }}
                {!! Form::close() !!}
            </div>
        </div>

    </div>
@endif

这是脚本代码

<div id="myModal" class="modal fade">
        <!-- Modal content -->
        <div class="modal-content">
            <span data-dismiss="modal" class="close">&times;</span>
            <p>Are you sure you wants to delete the promotion</p>
            {!! Form::open(['method'=>'DELETE','action'=>
     ['PromotionController@destroy',$promotion->id]]) !!}
            <div class="form-group">
     {{ Form::button('<i class="fa fa-trash-o"> Delete</i>', ['type' => 
   'submit', 'class' => 'btn btn-danger pull-right btn-sm','style'=>'margin-
   bottom:1em'] )  }}
                {!! Form::close() !!}
            </div>
        </div>

    </div>
@endif
    <script>

        $(document).ready(function () {
           $('#example1').DataTable({

                "ordering": false,

            });
        });

    </script>
<script>
    function showDeleteModal(id){
        $("#myModal").modal("show");


    }
</script>

这是控制器功能

public function destroy($id)
    {
        $promotion = Promotion::findOrFail($id);
        $promotion->delete($id);
        return redirect('admin/promotion');

    }
答案

你在$promotion->id结束后使用模态中的foreach,这就是为什么它总是存储last ID而你在form action中使用它需要让它变得动态。你可以使用Jquery来做到这一点。 showDeleteModal(this.id)也有错误

你需要更新你的showDeleteModal(this.id)功能

function showDeleteModal(id){
   var form = $("#myModal").find('form');
   var url = form.attr('action');
   url = url.split('/');
   url[url.length - 1] = id;
   url = url.join('/'); 
   form.attr('action',url);
   $("#myModal").modal("show"); 
}

<a href="#" class="myBtn" id="myBtn"onclick="showDeleteModal(this.id)">

<a href="#" class="myBtn" id="myBtn"onclick="showDeleteModal({{$promotion->id}})">

以上是关于如何在laravel中以模态运行控制器函数的主要内容,如果未能解决你的问题,请参考以下文章

UIAlertController 错误当在 swift 中以模态呈现的视图控制器中调用时

如何在 Laravel 中打开到模态窗口的路线

我该如何做模态对话框片段(代码在我关闭之前不会执行)

Laravel:如何在控制器的几种方法中重用代码片段

如何在 laravel 中以编程方式即时设置 .env 值

在导航堆栈中以模态方式呈现视图控制器