vendredi 3 juin 2022

insert multiple data based on checkbox selection in Laravel

I'm trying to insert multiple data based on checkbox selection in Laravel but when I submit the data it throws me this error:

Undefined index: select

  • My function that does the array insert of the selected checkboxes and request the data:
public function create(Request $request)
    {
        try {
            $data[] = request()->except('token_');
            for ($i = 0; $i < count($data['select']); $i++) {

                $consecutive = DB::select('SELECT SUM(certificationId) FROM certifications GROUP BY certificationId');
                $final_consecutive = sprintf("%04d", $consecutive);

                DB::table('certifications')->insert([
                    'student_id' => $data['student_id'][$i],
                    'consecutive' =>  $data['consecutive'][$i],
                    'year' => $data['year'][$i],
                    'season' => $data['season'][$i],
                    'program' => $data['program'][$i],
                    'course' => $data['course'][$i],
                    'level' => $data['level'][$i],

                ]);
            }
            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
      <form action="" method="POST">
                                

                                <td class="align-middle text-center">
                                    <input value="" name="consecutive[]">
                                    
                                </td>

                                <td class="align-middle text-center">
                                @foreach ($group->levels as $level)
                                    <input type="hidden" value="" id="year" name="year[]">
                                    
                              
                                </td>
                                <td class="align-middle text-center">
                               
                                    <input type="hidden" value="" name="season[]">
                                    
                                
                                </td>
                                <td class="align-middle text-center">
                              
                                    <input type="hidden" value="" name="program[]">
                                    
                              
                                </td>
                                <td class="align-middle text-center">
                                    <input type="hidden" value="" name="course[]">
                                    
                                </td>
                                <td class="align-middle text-center">
                            
                                    <input type="hidden" value="" name="level[]">
                                    
                              
                                </td>
                                <td class="align-middle text-center">
                               
                                    <input type="hidden" value="" name="duration[]">
                                    
                         
                                </td>
                                <td class="align-middle text-center">
                             
                                    <input type="hidden" value="" name="fechaInicio[]">
                                    
                               
                                </td>
                                <td class="align-middle text-center">
                           
                                    <input type="hidden" value="" name="endDate[]">
                                    
                               
                                </td>
                        @endforeach
                                <input class="form-control form-control-sm" type="hidden" name="student_id[]" value="" >

                                <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>

I also had something different in my create function. I put the index sutdent_id as index, something like this: foreach ($request->get('student_id') as $index => $student_id) but it didn't work either, it was the same error but with index student_id




Aucun commentaire:

Enregistrer un commentaire