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

jakegroves's avatar

Best way to do foreach input form

I know what i've done so far isn't correct, but trying to work out how to do it correctly, was hoping you guys could share insight on how to do it on laravel.

Basically on my view, it is retrieving all hte students in that lesson, and allowing you to take attendance for all the users. But i don't want to create a form for each student. But to create an array i guess?? So i can create new record in the database upon submission.

  {!! Form::open([
                        'url' => '/teacher/lessons/'. $lesson->id .'/attendance/new',
                        'method' => 'POST'
                    ]) !!}
                    {!! Form::hidden('lesson_id', $lesson->id) !!}
                        @foreach ($lesson->students as $ls)
                                {!! Form::label('attendance', $ls->name) !!}
                                {!! Form::select('attendance', [
                                                 'true' => 'Attended',
                                                 'false' => 'Absent'],
                                                 null,
                                                 ['class' => 'form-control'])
                                !!}
                                {!! Form::hidden('student_id', $ls->id) !!}
                        @endforeach
                    {!! Form::submit('Submit Class Attendance', [
                        'class' => 'btn btn-success'
                    ]) !!}
                    {!! Form::close() !!}

How would i structure the form to be able to do that??

0 likes
5 replies
jlrdw's avatar

Are you saying you just want a data entry form to submit, and the data entered goes to the database? Have you looked at the intermediate tutorial that's right in the docs, which can also be downloaded from Github. It would be similar to a new user form. Sorry if I mis-understand what you are after.

jakegroves's avatar

@jlrdw i've watched alot of tutorial vids, but what i am trying to achieve is that in the main form i have this:

Form open
               - array[0]
                             - lesson_id[value]
                     - attendance[value]
               - array[1]
                             - lesson_id[value]
                     - attendance[value]
               - array[2]
                             - lesson_id[value]
                     - attendance[value]
Form close

The arrays are generated from the foreach loop and then in the controller foreach the array's, i can do the rest but i can't seem to find or work out how to create arrays of values in forms

Does that make sense?? i didn't know how to explain, been searching for ages to do this

jlrdw's avatar

You shouldn't need array's like that, just post your form, the data will be in a request array anyway, and in your controller save the data to the database.
EDIT: Are you adding a record or editing a record? Either way the same, but some folks use put or patch if editing. You don't foreach on an add form.
EDIT 2: I think you want a list you can edit / update on the fly? Have you looked at datatables? I'd just write it myself, but look at jquery datables. It has inplace editing, to complex now to fully explain how to write your own, lot of code.

jakegroves's avatar

@jlrdw yeaaa i've looked at datatables i've already got that when i'm editing etc, but this inserting new just a normal POST.

right now in view-source my form is looking like this

<form method="POST" action="http://localhost:8000/teacher/lessons/1/attendance/new" accept-charset="UTF-8"><input name="_token" type="hidden" value="7FleFlR5Nu1lJZ6wkHtn16Ea73pT6VZ4pjjXP81r">
    <input name="lesson_id" type="hidden" value="1">
    <label for="attendance">student</label>
        <select class="form-control" id="attendance" name="attendance"><option value="true">Attended</option><option value="false">Absent</option></select>
        <input name="student_id" type="hidden" value="3">

        <label for="attendance">student2</label>
        <select class="form-control" id="attendance" name="attendance"><option value="true">Attended</option><option value="false">Absent</option></select>
        <input name="student_id" type="hidden" value="14">
        <input class="btn btn-success" type="submit" value="Submit Class Attendance">
</form>

and when i post 'student' and 'student2' as presnt and absent, all it is sending through is this:

{"_token":"7FleFlR5Nu1lJZ6wkHtn16Ea73pT6VZ4pjjXP81r","lesson_id":"1","attendance":"false","student_id":"14"}

but i kind of want this...

{"_token":"7FleFlR5Nu1lJZ6wkHtn16Ea73pT6VZ4pjjXP81r","lesson_id":"1","attendance":"false","student_id":"14"}
{"_token":"7FleFlR5Nu1lJZ6wkHtn16Ea73pT6VZ4pjjXP81r","lesson_id":"1","attendance":"true","student_id":"3"}

but i kind of think its not possible?? and i need to do a form for every single user right??

EDIT1: I could easily do this: where it's a form for every student but i was kinda hoping to avoid that:( as shown below:

  <table class="table table-borderless">
                        <tbody>
                            @foreach ($lesson->students as $ls)
                            <tr>
                                <td>{{ $ls->name }}</td>
                                <td>{!! Form::open([
                                    'url' => '/teacher/lessons/'. $lesson->id .'/attendance/new',
                                    'method' => 'POST'
                                ]) !!}
                                {!! Form::hidden('lesson_id', $lesson->id) !!}
                                {!! Form::select('attendance', [
                                                 'true' => 'Attended',
                                                 'false' => 'Absent'],
                                                  null,
                                                ['class' => 'form-control'])
                                !!}
                                {!! Form::hidden('student_id', $ls->id) !!}
                                {!! Form::submit('Submit Class Attendance', [
                                    'class' => 'btn btn-success'
                                ]) !!}
                                {!! Form::close() !!}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
d3xt3r's avatar

Why are you stuck with dropdown and arrays , use checkboxes (ticked for present, un-ticked for absent) and assign each checkbox with student id if that suites you.

Or you could give each select a different name as 'attendance-1' where 1 is the student id. In your controller , loop through all the request parameters to see wherever attendance is present.

Please or to participate in this conversation.