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

Time:03-21

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 >
                <input  type="text" name="name" value="{{$student->last_name}} {{$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">
             {{csrf_field()}}

                  <input  type="hidden" name="id_student" value="{{$student->id_student}}" >

                  <td >
                      <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("d", $consecutive); 
                foreach($request->select as $data)
                {
                    Records::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

CodePudding user response:

You have one form per $group->students which means only one should be inserted at a time. The fact you end up inserting many of the same is just problematic. If you truly want to insert multiple records at a time you need to wrap your @foreach in the form:

<form action="{{url('/Create/Records/')}}" method="POST">
{{csrf_field()}}
@foreach ($group->students as $student)
    @if($student->pivot->pass)
         <tr>
            <td >
                <input  type="text" name="name[]" value="{{$student->last_name}} {{$student->Names}}" disabled>
            </td>

            <td >
                <input  type="text" name="grade[]" value="{{isset($student->pivot->grade)?$student->pivot->grade:''}}" placeholder="Grade" disabled>
            </td>



            <input  type="hidden" name="id_student[]" value="{{$student->id_student}}" >

            <td >
                <input id="select" type="checkbox" name="select[]">
            </td>
        </tr>
    @endif
@endforeach
</form>

then you can iterate on the id_student:

    public function create(Request $request)
        {
            try { 
                foreach ($request->get('id_student') as $index => $id_student) {
                    $consecutive = DB::select('SELECT SUM(idRecord) FROM records GROUP BY idRecord'); 
                    $final_consecutive = sprintf("d", $consecutive); 
                    Records::create(['id_student' => $id_student, 'consecutive' => $final_consecutive]);
               }
              
                return back()->with('success', 'Constancia creada correctamente');
            } catch (\Illuminate\Database\QueryException $e) {
                // ....
            }
        }


Sidenote: I'm not sure why you are adding the name, grade and select in the form if you don't actually intend to use them.

  • Related