1
我從MySQL中拉出多個記錄,並循環瀏覽模型綁定,並用一些數據填充所有輸入字段。使用Laravel 5.3模型綁定更新多個記錄
現在,我可以更改2或10或全部26個字段並點擊更新按鈕。我想更新所有記錄。現在,我不知道$ id如何在這裏工作?通常我更新單個記錄,我有$ id,我可以找到並只更新該字段。 但這並非如此。我正在拉13條記錄(或26個字段)。 13場1和13場2。如何更新所有?
mycode的數據庫
Table
-id
-name
-field1 (updating this one)
-field2 (updating this one)
路線
Route::get('/cat' , '[email protected]');
Route::patch('/cat/{$id}/update','[email protected]_update');
控制器
public function cat(){
$cattfs = Catf::all();
return view('/cat',compact('cattfs'));
}
public function cat_update(Request $request, $id) // id = 1
{
$rules = array(
'field1' => 'required',
'field2' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect()->back()->withErrors($validator);
} else {
$cat = Cattf::find($id); //This wont work :/
$cat ->field1 = Input::get('field1');
$cat ->field2 = Input::get('field2');
$cat ->save();
return redirect('/cat');
}
}
查看
<div class="col-md-6" style="background-color:#fff">
<table class="table table-hover">
<thead>
<tr>
<th style="text-align: center">Product</th>
<th style="text-align: center">Pr</th>
<th style="text-align: center">Co</th>
</tr>
</thead>
<tbody>
@foreach ($cattos as $catto)
{!! Form::model($catto,[ 'method' =>'PATCH', 'url' => ['/cat/.$catto->id./update']]) !!}
<tr>
<td>{{$catto->name}}</td>
<td> {!! Form::text('field1' ,null , ['class'=>'form-control']) !!}</td>
<td> {!! Form::text('field2' ,null , ['class'=>'form-control']) !!}</td>
</tr>
@endforeach
<td colspan="3">
{!! Form::submit('UPDATE', ['class'=>'btn btn-primary btn-block']) !!}
{!! Form::close() !!}
</td>
</tr>
</tbody>
</table>
</div>
形式的快照
約束力還是可以的,那是顯示數據庫中以前的值是什麼? –