Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fromthedark_14's avatar

Passing an array values from views to controller

Hey everyone, can you help me again with this one? I want to get the entered grades of students and store it in an array and pass it to the controller.

student_id          grades
142                 98
444                 92
879                 85
343                 89
123                 95

I want to store those grades above like this $arrayGrd = [88, 92, 85, 89, 95

Here is my view:

{{Form::open(array('route'=>'update.grades', 'method' => 'post', 'files'=>true))}}
                                    {{ csrf_field() }}
                                        <tbody>
                                        @foreach($grades as $row)
                                            <tr>
                                                <td><input type="text" style="text-align: center" size="4" name=grades[]" multiple></td>
                                                <td></td>

                                            </tr>
                                        @endforeach
                                        </tbody>
                                    {{Form::close()}

Can you show me the logic? And also the code in the controller? I would be happy for your response. Thanks :)

0 likes
2 replies
lostdreamer_nl's avatar
Level 53

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:

  1. Get all users that can be graded.
  2. 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 ;)

Please or to participate in this conversation.