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("d", $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 >
<input type="text" name="nombre" value="{{$student->lastName}} {{$student->Names}}" disabled>
</td>
<td >
<input type="text" name="grade" value="{{isset($student->pivot->grade)?$student->pivot->grade:''}}" placeholder="grade" disabled>
</td>
<form action="{{url('/Create/Records')}}" method="POST">
<input type="hidden" name="id_student" value="{{$student->id_student}}" >
<td >
<input id="select" type="checkbox" name="select[]">
</td>
</tr>
@endforeach
</tbody>
</table>
<div >
<button type="submit" >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
CodePudding user response:
You should pass data to your Model like:
Records::create(['id_student' => $idStudent, 'consecutive' => $consecutive]);
Currently, you are giving like:
Records::create('string');
Which is not correct way to pass the data. That's why getting the error.
CodePudding user response:
Create() take array as parameter. When your foreach loop execute $data value 0 or 1 as string so that why error is given.
if you want to create in foreach loop then
foreach($request->select as $data)
{
Records::create(['field_name' => $data]);
}
CodePudding user response:
by default multiple select checkbox will be converted to assoc array with name that ends with [] as its key for example your using so you can access it like:
$select = $_POST['select'];
then you can iterate through the values this is a plain php would suggest to use the laravel method as it offers more like security and filter this one is a simple and fast solution. But if you using an ajax submission you should always make sure that the parameter always ends with [] example you have a get request but still the same syntax applies to post method:
http://localhost?select[]=1&select[]=2
the same code will work as on top but for get request like this you will use:
$select = $_GET['select'];
haven't tried on laravel request object but I think the same conversion is being done when your using the Request instance you may try that as well just make sure you always end the parameter name with [] but if the name of all the checkbox is set to select it will just return a single string which the error means.