如何 运行 laravel 中的模态控制器功能

how to run a controller function in modal in laravel

我想做的是 运行 模式中的控制器删除功能,当我尝试使用该删除功能时它在循环的末尾,它获取最后一个 ID 并将其删除,我希望它获取调用该函数的当前 ID。我应该做什么??我不能将模型代码放在 CSS 文件中,因为它会干扰其他样式,因此脚本代码应该在 blade 视图中

@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');

    }

您在 foreach 结束后在模态中使用 $promotion->id,这就是为什么它总是存储 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}})">