dimanche 20 mars 2022

Laravel 7 - No data mapped in insert multiple data to mysql database

I am inserting multiple data into mysql database, apparently everything works fine, but when I checked the data in phpmyadmin, it seems that only the first value of the first selected checkbox is being inserted multiple times, which means only the first id_student is the value that is being inserted multiple times. I think it's a mapping problem but I don't know how to solve it, can someone help me?

  • This is my view
@foreach ($group->students as $student)
         @if($student->pivot->pass)
         <tr>
            <td class="align-middle text-center">
                <input class="form-control" type="text" name="name" value="  " disabled>
            </td>

            <td class="align-middle text-center">
                <input class="form-control form-control-sm" type="text" name="grade" value="" placeholder="Grade" disabled>
            </td>

            <form action="" method="POST">
             

                  <input class="form-control form-control-sm" type="hidden" name="id_student" value="" >

                  <td class="align-middle text-center">
                      <input id="select" type="checkbox" name="select[]">
                  </td>
        </tr>
        @endif
@endforeach
  • This is my function in Controller.

     public function create(Request $request)
        {
            try { 
                
                $id_student = $request->get('id_student');
                   
                $consecutive = DB::select('SELECT SUM(idRecord) FROM records GROUP BY idRecord'); 
                $final_consecutive = sprintf("%04d", $consecutive); 
                foreach($request->select as $data)
                {
                    Constancias::create(['id_student' => $id_student, 'consecutive' => $final_consecutive]);
                }
    
                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);
            }
        }

  • Image of the table "Record"

records




Aucun commentaire:

Enregistrer un commentaire