我有一個表格來編輯和更新學生的某些值,比如成績、獎學金、評論等。我有一個@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 hljs-string">"><input name="InvoiceNumber" type="text" value="{{isset($student->pivot->InvoiceNumber)?$student->pivot->InvoiceNumber:''}}" hljs-string">" placeholder="Factura"></td>
<td hljs-string">"><input name="grade" type="text" value="{{isset($student->pivot->grade)?$student->pivot->grade:''}}" hljs-string">" placeholder="grade"></td>
<td hljs-string">"><textarea name="Comments" type="text" hljs-string">" placeholder="Comments" rows="1">{{isset($student->pivot->Comments)?$student->pivot->Comments:''}}</textarea></td>
<td hljs-string">">
<button type="submit" hljs-string">"> @lang('show.Save')<i hljs-string">" style="font-size:20px">save</i></button>
</td>
<td hljs-string">">
<form method="post" action="{{url('Groups/Disenroll/'.$group->idGroup.'?idStudent='.$student->id_student)}}">
{{csrf_field()}}
<button type="submit" hljs-string">" > <i hljs-string">" style="font-size:10px">call_split</i></button>
</form>
</td>
</tr>
@endforeach
一切正常,但視圖總是在保存其中一個學生的資料后重新加載,因此快速逐個學生保存以不洗掉其他學生的修改資訊有點繁瑣。
怎么可能修改?使表單發送所有資料而不一一保存。
uj5u.com熱心網友回復:
您可以使用jquery ajax創建發布請求。那么你可以在不重繪 頁面的情況下更新資料庫
uj5u.com熱心網友回復:
你必須使用:
<input {{$student->pivot->scolarship=='1'?'checked':''}} type="checkbox" name="scolarship[]" value="1"></td>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/408868.html
標籤:
下一篇:反應動態表單輸入
