2014-06-07 39 views
0

我有一個包含設置列表的表單,所有設置都有自己的ID,通過簡單的foreach循環顯示。當用戶點擊更新按鈕時,我希望能夠使用Anahkiasen/Former更新所有的值。但是,我似乎無法找到關於如何提交多個記錄集的文檔。使用Laravel中的前一次更新多個行

舉例來說,如果我打開表單編輯只是一個設定,而不是所有的人,我只想做到以下幾點:

{{ Former::open()->method('PUT')->action(URL::to('settings/' . $setting['id'])) }} 

這將其發送給我的「更新」資源的方法和下面的代碼會照顧的更新:

public function update($id) 
{ 
    $setting = Setting::findOrFail($id); 

    if ($setting->save()) { 
     return Redirect::back()->with('message', "Setting #$id updated!"); 
    } else { 
     return Redirect::back()->withInput()->withErrors($setting->errors()); 
    } 
} 

是否可以同時做這與多個記錄,並仍然使用便捷$autoHydrateEntityFromInput$forceEntityHydrationFromInput功能?

我正在使用Laravel 4.1和3.4.2。

回答

0

這不得不通過只把值到一個數組,並在刀片模板迫使現有的值來完成:

@foreach ($settings as $s) 
<div class="row"> 
    {{ Former::text('values[' . $s['id'] . ']', $s['name'])->forceValue($s['value']) }} 
</div> 
@endforeach 

然後在控制器中檢索值:

public function update() 
{ 
    $values = Input::get('values'); 

    foreach ($values as $id => $val) { 
     $setting = Setting::findOrFail($id); 

     $setting['value'] = $val; 

     if (!$setting->save()) { 
      return Redirect::back()->withInput()->withErrors($setting->errors()); 
     } 
    } 

    return Redirect::back()->with('message', "Settings updated!"); 
} 

做這樣,一個標準的資源將不會像Route::resource('settings', 'SettingsController');那樣工作,所以我不得不簡單地做Route::post('settings/update', '[email protected]');

據我所知,汽車九頭蛇在這種情況下,灰色也不起作用。