php - Laravel: inserting arrays to a table -
i got code can store result(present,late,absent,others) each student.
here's view
<div align="center"><b>list of students enrolled</b></div> <table class="table table-striped table-bordered"> <thead> <tr> <th>student id</th> <th>student name</th> <th>present</th> <th>late</th> <th>absent</th> <th>others</th> <th>comments</th> </tr> </thead> <tbody> @foreach ($users $users) <tr> <td>{{ $users->student_id }} </td> <td>{{ $users->student_firstname }} {{ $users->student_lastname }}</td> <td>{{ form::radio('student['.$users->student_id.'][status]', 'present' , true) }}</td> <td>{{ form::radio('student['.$users->student_id.'][status]', 'late' ) }}</td> <td>{{ form::radio('student['.$users->student_id.'][status]', 'absent') }}</td> <td>{{ form::radio('student['.$users->student_id.'][status]', 'others') }}</td> <td>{{ form::text('student['.$users->student_id.'][comment]') }}</td> @endforeach </tr> {{ html::script('js/bootstrap.js'); }} </tbody> </table>
here's controller save
foreach(subject1::where('section_name', 'like', input::get('section_name'))->where('code','like',input::get('subject_code'))->get() $student){ foreach (input::get('student') $studentid ) { $attendance = new attendances(); $attendance->status = $studentid['status']; $attendance->comment = $studentid['comment']; $attendance->student_id = $student->student_id; $attendance->student_firstname = $student->student_firstname; $attendance->student_lastname = $student->student_lastname; $attendance->teacher_id = auth::user()->id; $attendance->teacher_firstname = auth::user()->firstname; $attendance->teacher_lastname = auth::user()->lastname; $attendance->section_id = $student->section_id; $attendance->section_name = input::get('section_name'); $attendance->subject_id = $student->id; $attendance->subject_code = $student->code; $attendance->subject_name = $student->name; $attendance->date_recorded = input::get('date'); $attendance->save(); } }
however, when saw table in database shows 2 records student1 has present , absent on day student2 (assuming input present on student1 , absent on student2
please :(
it appears loop through several students input incorporate student model outside loop. issue is.
aside that, should store student id on attendance table rather student data first/last name. consider how mucked things if has name change.
Comments
Post a Comment