一次保存多个表行(Laravel 4)

时间:2017-06-17 15:35:25

标签: php sql laravel laravel-4

我有一个表从数据库中获取数据,而不是我需要一次更新所有行。在每个单元格中,我添加了输入字段。现在我希望能够在输入数据时立即更新所有用户,但我不知道如何。

以下是我的观点图片:

View

enumerable:false

//控制器

@extends('admin/master')
@section('content')

<section class="content">
    {{Form::open()}}
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <div class="col-lg-4">
                        <h3 class="box-title">INPUT EXAM RESULTS FOR EACH STUDENT: </h3>
                    </div>
                    <div class="pull-right col-lg-8">
                        <div class="col-lg-2 col-lg-offset-5 pull-left">
                            <select class="btn bg-navy" name="city" >
                                <option>Select City</option>
                                <option>Prishtin</option>
                                <option>Prizren</option>
                            </select>
                        </div>
                        <div class="col-lg-5 pull-right">
                             <div class="input-group">
                                <input type="text" name="search_input" class="form-control" placeholder="Search here...">
                                <div class="input-group-btn">
                                    <button type="submit" class="btn btn-info"><i class="fa fa-search"></i> Search</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="box-body">
                    <?php if (isset($data)) { ?>
                    <table id="example2" class="table table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th>{{Lang::get('messages.stid')}} </th>
                                    <th>{{Lang::get('messages.name')}}</th>
                                    <th>Subject 1</th>
                                    <th>Subject 2</th>
                                    <th>Subject 3</th>
                                    <th>Subject 4</th>
                                </tr>
                            </thead>
                            <tbody>@foreach ($data as $row)
                                <tr>
                                    <td>{{$row->id}}</td>
                                    <td>{{$row->fname}} {{$row->lname}}</td>
                                    <td><input type="text" name="sub1" class="form-control" placeholder="Add marks here..."></td>
                                    <td><input type="text" name="sub2" class="form-control" placeholder="Add marks here..."></td>
                                    <td><input type="text" name="sub3" class="form-control" placeholder="Add marks here..."></td>
                                    <td><input type="text" name="sub4" class="form-control" placeholder="Add marks here..."></td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    <button type="submit" class="col-lg-2 pull-right btn btn-success"><i class="fa fa-save"></i> Save</button>
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
    {{Form::close()}}
</section>

{{ HTML::script('/admin/assets/js/jquery-2.2.3.min.js') }}
{{ HTML::script('/admin/assets/js/bootstrap.min.js') }}
{{ HTML::script('/admin/assets/js/jquery.dataTables.min.js') }}
{{ HTML::script('/admin/assets/js/dataTables.bootstrap.min.js') }}

<script>
    $(function () {
        $("#example1").DataTable();
        $('#example2').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false,
            "ordering": true,
            "info": true,
            "autoWidth": false
        });
    });
</script>

@stop

1 个答案:

答案 0 :(得分:0)

您的输入元素必须是数组,由$row->id键入(我假设这是学生表的主键),以识别该行所代表的学生。例如:

<td><input type="text" name="sub1[{{ $row->id}}]" class="form-control" placeholder="Add marks here..."></td>

然后,当您提交数据时,按数组键查找学生并相应地更新其信息。有什么影响:

$key = // extract id from sub1 array
Student::find($request->input($key));
// do updates here
// repeat for each sub input...