简体   繁体   中英

Laravel 7 - Form

I have a form to edit and update certain values of a student, like grade, scholarship, comments, etc. And I have a @foreach so that I can update each student one by one, but I want to change that, I want to send all data of all students at the end, not one by one. Based on what I have, can I do that?

  • StudentController

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

  • View

   
  @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

Everything works fine but the view always reloads after saving the data of one of the students, so it is a bit tedious to be saving student by student quickly so that the modified information of the other students is not erased.

How could it be modified? Make the form send all the data without saving one by one.

You can create a post request using jquery ajax . So then you can update database without refreshing the page

you must use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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