[ad_1]
I’m trying to insert the selected data with checkboxes but when I submit the dat it throws me this error:
Invalid argument supplied to foreach().
It worked before when I had the first foreach and the variable $consecutive_final
- My function that does the array insert of the selected checkboxes and request the data
public function create(Request $request)
{
try {
foreach ($request->get('student_id') as $index => $student_id) {
foreach ($request->get('anio') as $index => $anio) {
foreach ($request->get('season') as $index => $season) {
foreach ($request->get('program') as $index => $program) {
foreach ($request->get('course') as $index => $course) {
foreach ($request->get('level') as $index => $level) {
foreach ($request->get('grade') as $index => $grade) {
$consecutive = DB::select('SELECT SUM(certificationId) FROM certifications GROUP BY certificationId');
$final_consecutive = sprintf("%04d", $consecutive);
Certifications::create([
'student_id' => $student_id,
'consecutive' => $final_consecutive,
'year' => $year,
'season' => $season,
'program' => $program,
'course' => $course,
'level' => $level,
'grade' => $grade,
]);
}
}
}
}
}
}
}
return back()->with('success', 'Constancia creada correctamente');
} catch (\Illuminate\Database\QueryException $e) {
$message = $e->getMessage();
if (strpos($message, "Duplicate entry")) {
return back()->with('err', 'Esta constancia ya ha sido creada');
}
if (strpos($message, "1366 Incorrect integer value: '' for column 'idGrupo'")) {
return back()->with('err', 'Debe seleccionar un grupo para poder continuar');
}
return back()->with('err', $message);
}
}
- And this is my form where the inputs don’t actually ask for information, but I map it
<form action="{{url('/Create/Certification/')}}" method="POST">
{{csrf_field()}}
<td class="align-middle text-center">
<input class="form-control form-control-sm" type="text" name="grade[]"
value="{{isset($student->pivot->grade)?$student->pivot->grade:''}}" placeholder="grade" disabled>
</td>
<td class="align-middle text-center">
@foreach ($group->levels as $level)
<input type="hidden" value="{{ $nivel->course->program->season->year->year }}" id="year" name="year[]">
{{$level->course->program->season->year->year}}
@endforeach
</td>
<td class="align-middle text-center">
@foreach ($group->levels as $level)
<input type="hidden" value="{{ $level->course->program->season->seasonName }}" name="season[]">
{{$level->course->program->season->seasonName}}
@endforeach
</td>
<td class="align-middle text-center">
@foreach ($group->levels as $level)
<input type="hidden" value="{{ $level->course->program->programName }}" name="program[]">
{{$level->course->program->programName}}
@endforeach
</td>
<td class="align-middle text-center">
@foreach ($group->levels as $level)
<input type="hidden" value="{{ $level->course->courseName }}" name="course[]">
{{$level->course->courseName}}
@endforeach
</td>
<td class="align-middle text-center">
@foreach ($group->levels as $level)
<input type="hidden" value="{{ $level->levelName }}" name="level[]">
{{$level->levelName}}
@endforeach
</td>
<input class="form-control form-control-sm" type="hidden" name="student_id[]" value="{{$student->student_id}}" >
<td class="align-middle text-center">
<input id="select" type="checkbox" name="select[]">
</td>
</tr>
@endif
@endforeach
</tbody>
</table>
<div class="d-flex justify-content-center">
<button type="submit" class="btn btn-sm btn-outline-success">Create</button>
</div>
</form>
[ad_2]