Laravel 7 - 将多个数据插入 mysql 数据库时没有数据映射

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

我正在向 mysql 数据库中插入多个数据,显然一切正常,但是当我在 phpmyadmin 中检查数据时,似乎只有第一个选中的复选框的第一个值被多次插入,这意味着只有第一个 id_student 是被多次插入的值。我认为这是一个映射问题,但我不知道如何解决,有人可以帮助我吗?

@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="{{$student->last_name}} {{$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">
             {{csrf_field()}}

                  <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>
        @endif
@endforeach

     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)
                {
                    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);
            }
        }

每个 $group->students 有一个表格,这意味着一次只能插入一个。您最终插入许多相同的事实只是有问题的。如果您真的想一次插入多条记录,您需要将 @foreach 包装成以下形式:

<form action="{{url('/Create/Records/')}}" method="POST">
{{csrf_field()}}
@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="{{$student->last_name}} {{$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>



            <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>
    @endif
@endforeach
</form>

然后你可以迭代 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("%04d", $consecutive); 
                    Records::create(['id_student' => $id_student, 'consecutive' => $final_consecutive]);
               }
              
                return back()->with('success', 'Constancia creada correctamente');
            } catch (\Illuminate\Database\QueryException $e) {
                // ....
            }
        }


旁注:如果您实际上并不打算使用它们,我不确定您为什么要在表单中添加 namegradeselect