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 >' + n + '</td>' + '<td><input type="text" name="name[]" value="{{ old('name') }}"></td>'+ '<td><input type="text" name="fname[]" value="{{ old('fname') }}"></td>'+ '<td><input type="text" name="rollno[]" value="{{ old('rollno') }}"></td>'+ '<td><input type="text" name="obtainedmarks[]" value="{{ old('email') }}"></td>'+ '<td><input type="text" name="totalmarks[]"></td>'+ '<td><input type="text" name="percentage[]"></td>'+ '<td><input type="button" value="x"></td></tr>'; $('.resultbody').append(tr); }); $('.resultbody').delegate('.delete','click',function () { $(this).parent().parent().remove(); }); });</script><div > <div > <div > <div > <div >Add Results</div> <div > <form role="form" method="POST" action="{{ url('/result') }}"> {!! csrf_fIEld() !!} <table > <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 > <tr> <td >1</td> <td> <input type="text" name="name[]" value="{{ old('name') }}"> </td> <td> <input type="text" name="fname[]" value="{{ old('fname') }}"> </td> <td> <input type="text" name="rollno[]" value="{{ old('rollno') }}"> </td> <td> <input type="text" name="obtainedmarks[]" value="{{ old('email') }}"> </td> <td> <input type="text" name="totalmarks[]"> </td> <td> <input type="text" name="percentage[]"> </td> <td> <input type="button" value="x"> </td> </tr> </tbody> </table> <center><input type="button" value="Add New Item"> <input type="submit" 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');}总结
以上是内存溢出为你收集整理的php – 如何使用Laravel Eloquent将多个记录插入数据库全部内容,希望文章能够帮你解决php – 如何使用Laravel Eloquent将多个记录插入数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)