Is this in laravel 4? If not, you're using the wrong braces ( {{ }} instead of {!! !!} ) for echo'ing HTML codes
I think something like this should work, but I'm not sure without seeing the Grade model or it's table.
I'm now assuming the Grade model / table has the 2 fields: 'student_id' and 'grade' (integer?)
{{Form::open(array('route'=>'update.grades', 'method' => 'post', 'files'=>true))}}
{{ csrf_field() }}
<table>
<thead>
<tr>
<th>Student ID</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
@foreach($grades as $grade)
<tr>
<td>{{ $grade->student_id }}</td>
<td><input type="text" style="text-align: center" size="4" name="grades[{{ $grade->student_id }}]" value="{{ $grade->grade }}"></td>
</tr>
@endforeach
</tbody>
</table>
{{ Form::close() }}
This should then output a list of input fields with all grades that where set. But I think you want it a bit differently:
- Get all users that can be graded.
- Show a list of all user's name + an input field to add the grade (or if there was a grade already, show the current grade so you can update it)
If that is the case, You could do so like this:
GradeController.php:
public function index()
{
$students = Student::get();
$grades = Grade::get()->keyBy('student_id');
return view('grade-list', compact('students', 'grades'));
}
public function update(Request $request)
{
$grades = $request->grades;
// Filter all values that are empty (not filled in)
$grades = array_filter($grades, function($grade) { return $grade !== ''; });
dd($grades);
}
views/grade-list.blade.php:
{{Form::open(array('route'=>'update.grades', 'method' => 'post', 'files'=>true))}}
{{ csrf_field() }}
<table>
<thead>
<tr>
<th>Student</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
@foreach($students as $student
<tr>
<td>{{ $student->name }}</td>
@if($grades->has($student->id)
<td><input type="text" style="text-align: center" size="4" name="grades[{{ $student->id }}]" value="{{ $grades[$student->id]->grade }}"></td>
@else
<td><input type="text" style="text-align: center" size="4" name="grades[{{ $student->id }}]"></td>
@endif
</tr>
@endforeach
</tbody>
</table>
{{ Form::close() }}
This would show a list of all users. If they have a grade, it will be prefilled, if they dont, the value will be empty. After submit (if the update() method is linked to the update.grades route) you will get an array of all students that have had a grade set in the form.
Ask back if you have follow up questions ;)