如何使用Laravel Eloquent将多个记录插入数据库

我正在开发一个Web应用程序,出现了一个场景,用户可以一次插入多个记录。我在route result/create处创建了一个表单,用户可以在前端添加多行,现在当用户单击提交时,我希望所有记录/行数据以数组形式插入到数据库中。现在,当我点击提交时,我得到这个错误Undefined index: id

PS:我正在使用Laravel 5.2和资源控制器。

添加记录视图:

@extends('layouts.app')
@section('content')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('.add').click(function () {
            var n = ($('.resultbody tr').length - 0) + 1;
            var tr = '<tr><td class="no">' + n + '</td>' +
                    '<td><input type="text" class="name form-control" name="name[]" value="{{ old('name') }}"></td>'+
                    '<td><input type="text" class="fname form-control" name="fname[]" value="{{ old('fname') }}"></td>'+
                    '<td><input type="text" class="rollno form-control" name="rollno[]" value="{{ old('rollno') }}"></td>'+
                    '<td><input type="text" class="obtainedmarks form-control" name="obtainedmarks[]" value="{{ old('email') }}"></td>'+
                    '<td><input type="text" class="totalmarks form-control" name="totalmarks[]"></td>'+
                    '<td><input type="text" class="percentage form-control" name="percentage[]"></td>'+
                    '<td><input type="button" class="btn btn-danger delete" value="x"></td></tr>';
            $('.resultbody').append(tr);
        });
        $('.resultbody').delegate('.delete', 'click', function () {
            $(this).parent().parent().remove();
        });
    });
</script>
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">Add Results</div>

                <div class="panel-body">
                   <form class="form-horizontal" role="form" method="POST" action="{{ url('/result') }}">
                        {!! csrf_field() !!}
                    <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Student Name</th>
                                    <th>Father Name</th>
                                    <th>Roll No</th>
                                    <th>Obtained Marks</th>
                                    <th>Total Marks</th>
                                    <th>%</th>
                                    <th>Delete</th>
                                </tr>
                            </thead>
                            <tbody class="resultbody">
                                <tr>
                                    <td class="no">1</td>
                                    <td>
                                        <input type="text" class="name form-control" name="name[]" value="{{ old('name') }}">
                                    </td>
                                    <td>
                                        <input type="text" class="fname form-control" name="fname[]" value="{{ old('fname') }}">
                                    </td>
                                    <td>
                                        <input type="text" class="rollno form-control" name="rollno[]" value="{{ old('rollno') }}">
                                    </td>
                                    <td>
                                        <input type="text" class="obtainedmarks form-control" name="obtainedmarks[]" value="{{ old('email') }}">
                                    </td>
                                    <td>
                                        <input type="text" class="totalmarks form-control" name="totalmarks[]">
                                    </td>
                                    <td>
                                        <input type="text" class="percentage form-control" name="percentage[]">
                                    </td>
                                    <td>
                                        <input type="button" class="btn btn-danger delete" value="x">
                                    </td>
                                </tr>

                            </tbody>
                        </table>    
                        <center><input type="button" class="btn btn-lg btn-primary add" value="Add New Item">   
                        <input type="submit" class="btn btn-lg btn-default" value="Submit"></center>
                        </form>
                </div>
            </div>
        </div>

    </div><!-- First Row End -->
</div> <!-- Container End -->

@endsection 

学生管理员:

public function store(Request $request)
    {
        $input = Input::all();
        $condition = $input['id'];
        for($id = 0; $id<$condition; $id++){
            $student = new Student;
            $student->name = $input['name'][$id];
            $student->save();
        }
        return view('students.index');
    }

你得到这个错误,因为你没有id表单字段。 尝试这个:

public function store(Request $request)
{
    $input = Input::all();
    $condition = $input['name'];
    foreach ($condition as $key => $condition) {
        $student = new Student;
        $student->name = $input['name'][$key];
        $student->fname = $input['fname'][$key];
        $student->rollno = $input['rollno'][$key];
        $student->obtainedmarks = $input['obtainedmarks'][$key];
        $student->totalmarks = $input['totalmarks'][$key];
        $student->percentage = $input['percentage'][$key];
        $student->save();
    }
    return view('students.index');
}
链接地址: http://www.djcxy.com/p/61457.html

上一篇: How to insert multiple records to database using Laravel Eloquent

下一篇: How (and where) to construct an Entity with large number of params?