Laravel可以在索引视图中执行更新/编辑吗?

时间:2018-07-16 08:20:40

标签: php laravel bootstrap-modal

是否可以使用索引视图使用模式表单同时执行更新/编辑?我有索引视图,其中的表显示了数据,并且在表内部有名为[编辑和删除]的按钮,现在我想在编辑按钮被按下后执行编辑/更新。单击,然后将出现模态形式。但是无论何时使用模态形式,它都会显示如下图所示的错误。enter image description here

这是我的控制器:

public function show_setup()
{
    $batch=Batch::all();

    return view('setup.show_batch',compact('batch'));
}

public function edit_batch(request $request)
{
        $batch = Batch::find ($request->id);
        $batch->batch_name = $request->batch_name;
        $batch->save();

        return redirect()->back();

}

我的观点

<form>
    <div class="form-group">
        <table class="table table-hover table-bordered" id="table">
            <tr>
                <th>Batch</th>
                <th>Action</th>
            </tr>

            @foreach($batch as $bt)
            <tr>
                <td>{{$bt->batch_name}}</td>
                <td>
                    <a href="#" data-toggle="modal" data-target="#edit_batch" class="btn btn-info btn-sm">Edit </a>
                    <a href="#" class="btn btn-danger btn-sm">Delete</a>
                </td>
            </tr>
            @endforeach
        </table>
    </div>

</form>   <!-- Modal-->
<div id="edit_batch" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" class="modal fade text-left">
    <div role="document" class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 id="exampleModalLabel" class="modal-title">Edit Batch</h5>
                <button type="button" data-dismiss="modal" aria-label="Close" class="close">
                <span aria-hidden="true">×</span></button>
            </div>
            <div class="modal-body">                        
                <form action="setup/batch/edit" method="POST">
                    {{csrf_field()}}
                    {{ method_field('PUT') }}

                    <div class="form-group">
                        <label>School Year</label>
                        <input type="text" placeholder="School Year" name="batch_name" value="{{$batch->batch_name}}" class="form-control" autocomplete="off">
                    </div>
                </div>
                    <div class="modal-footer">
                      <button type="button" data-dismiss="modal" class="btn btn-secondary">Close</button>
                      <button type="submit" class="btn btn-primary">Save changes</button>
                    </div>
                </form>
                  </div>
                </div>
            </div>
       <!--End batch Modal-->

1 个答案:

答案 0 :(得分:0)

问题是您要从集合batch_name中获取$batch,您必须使用javaScript传递所需的数据,而所需的数据是id和{ {1}}

这里是一个例子:

batch_name