繁体   English   中英

Laravel 7 - 表格

[英]Laravel 7 - Form

我有一个表格来编辑和更新学生的某些值,比如成绩、奖学金、评论等。我有一个@foreach ,这样我就可以一个一个地更新每个学生,但我想改变它,我想最后发送所有学生的所有数据,而不是一一发送。 根据我所拥有的,我可以这样做吗?

  • 学生控制器

    public function enroll($id_student, request $request)
        {
            try {
                $idGroup = $request->get('idGroup');
                $student = Students::where('id_student', $id_student)->first();
                $student->groups()->attach([$idGroup => ['InvoiceNumber' => $request->get('InvoiceNumber'), 'Comments' => $request->get('Comments'), 'scolarship' => $request->get('scolarship') ? $request->get('scolarship') : 0]]);
                return back()->with('success', 'Student successfully enrolled');
            } catch (\Illuminate\Database\QueryException $e) {
                $message = $e->getMessage();
                if (strpos($message, "Duplicate entry")) {
                    return back()->with('err', 'This Student is already registered in this group');
                }
                if (strpos($message, "1366 Incorrect integer value: '' for column 'idGrupo'")) {
                    return back()->with('err', 'You must select a group to continue');
                }
                return back()->with('err', $message);
            }
        }
    
        public function enrollEdit($id_student, $idGroup, Request $request)
        {
            try {
                $student = Students::where('id_student', $id_student)->first();
                $group = $student->groups()->where('groups.idGroup', $idGroup)->first();
                $group->pivot->NumeroDeFactura = $request->get('NumeroDeFactura');
                $group->pivot->Comentarios = $request->get('Comentarios');
                $group->pivot->beca = $request->get('beca') ? $request->get('beca') : 0;
                $group->pivot->calificacion = $request->get('calificacion');
                $group->pivot->save();
                return back()->with('success', 'Enrollment edited successfully');
            } catch (\Throwable $th) {
                return back()->with('err', 'Error: ' . $th->getMessage());
                //throw $th;
            } catch (\Illuminate\Database\QueryException $e) {
                $mensaje = "There is already a student with that registered email";
                return back()->with('err', $mensaje);
            }
        }

  • 看法

   
  @foreach ($group->students as $student)
     <tr>
        <td class="align-middle ">
            @if ($student->pivot->scolarship == 0 && $student->pivot->InvoiceNumber == '')<!--Validacion para imprimir icono de no pago-->
                <i class="material-icons text-danger">payment</i>
            @endif
            <i class="material-icons">{{$student->pivot->scolarship?'school':''}}</i>
            {{$student->Last_Name}} {{$student->Apellido_Materno}} {{$student->Names}}
        </td>
        <td class="align-middle text-center">
            <form method="post" action="{{url('Students/enroll/'.$student->id_student.'/'.$group->idGroup.'/edit')}}">  
                 {{csrf_field()}}
                 <input  {{$student->pivot->scolarship=='1'?'checked':''}} type="checkbox" name="scolarship" value="1"></td>
                 <td class="align-middle text-center"><input name="InvoiceNumber" type="text" value="{{isset($student->pivot->InvoiceNumber)?$student->pivot->InvoiceNumber:''}}" class="form-control form-control-sm" placeholder="Factura"></td>
                 <td class="align-middle text-center"><input name="grade" type="text" value="{{isset($student->pivot->grade)?$student->pivot->grade:''}}" class="form-control form-control-sm" placeholder="grade"></td>
                 <td class="align-middle text-center"><textarea name="Comments" type="text"  class="form-control form-control-sm" placeholder="Comments"  rows="1">{{isset($student->pivot->Comments)?$student->pivot->Comments:''}}</textarea></td>
                  <td class="align-middle text-center">
                      <button type="submit"  class="btn btn-sm btn-outline-warning"> @lang('show.Save')<i class="material-icons" style="font-size:20px">save</i></button>
                  </td>

                  <td class="align-middle text-center">
                       <form method="post" action="{{url('Groups/Disenroll/'.$group->idGroup.'?idStudent='.$student->id_student)}}">
                             {{csrf_field()}}
                             <button type="submit"  class="btn btn-sm btn-outline-danger"  > <i class="material-icons" style="font-size:10px">call_split</i></button>
             </form>
                 </td>
     </tr>
  @endforeach

一切正常,但视图总是在保存其中一个学生的数据后重新加载,因此快速逐个学生保存以不删除其他学生的修改信息有点繁琐。

怎么可能修改? 使表单发送所有数据而不一一保存。

您可以使用jquery ajax创建发布请求。 那么你可以在不刷新页面的情况下更新数据库

你必须使用:

<input  {{$student->pivot->scolarship=='1'?'checked':''}} type="checkbox" name="scolarship[]" value="1"></td>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM