I am trying to insert data into database. But i don't know how to insert multi select checkbox data into the mysql database.
- Controller
public function create(Request $request)
{
try {
$id_student = $request->get('id_student');
$consecutive = DB::select('SELECT SUM(idRecord) FROM record GROUP BY idRecord');
$final_consecutive = sprintf("%04d", $consecutive);
foreach($request->select as $data)
{
Records::create($data);
}
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);
}
}
- View
@foreach ($group->students as $student)
<tr>
<td class="align-middle text-center">
<input class="form-control" type="text" name="nombre" value="{{$student->lastName}} {{$student->Names}}" disabled>
</td>
<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>
<form action="{{url('/Create/Records')}}" method="POST">
<input class="form-control form-control-sm" type="hidden" name="id_student" value="{{$student->id_student}}" >
<td class="align-middle text-center">
<input id="select" type="checkbox" name="select[]">
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="d-flex justify-content-center">
<button type="submit" class="btn btn-sm btn-outline-success">Save</button>
</div>
What I tried to do was use a select checkbox and then in the Controller I passed it as an array in the foreach loop, but honestly I think I'm nowhere close to figuring it out... Other than that the consecutive is automatically generated and I don't know how to pass it to the array as well.
I get this error by the way:
Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, string given
