Ich entwickle eine Webanwendung, es kommt ein Szenario, in dem Benutzer mehrere Datensätze gleichzeitig einfügen können. Ich erstellte ein Formular auf Route result/create
, wo Benutzer mehrere Zeilen im Frontend hinzufügen können, Jetzt, wenn der Benutzer auf "Senden" klicke, möchte ich alle Datensätze/Zeilen-Daten, die in Array-Form in die Datenbank eingefügt werden. Jetzt, wenn ich auf "Senden" klicke, erhalte ich diesen Fehler Undefined index: id
.Wie man mehrere Datensätze mit Laravel Eloquent in die Datenbank einfügt
PS: Ich benutze Laravel 5.2 und Resource Controller.
Hinzufügen von Datensätzen anzeigen:
@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
Studenten Controller:
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');
}
Error in Students.php Linie 48: Undefined offset: 0 Ich erhalte diesen Fehler –
Ah, ich hatte die 'key's rückwärts. Siehe die Änderung. – Samsquanch
Arbeitete wie Charme Prost! –