2015-01-21 56 views
1

我的項目是關於屬於多個部分的商店。 創建商店時,在多選列表中選擇(選擇)部分,並正確保存值。在編輯信息時,用戶可以正確保存更改。問題是:在編輯表單中,多選對象清晰(根本沒有選項),而我們需要根據預先保存的數據設置要選擇的選項,以便用戶查看和修改它。在Laravel 4中設置多選對象的選定項目

在控制器

我選擇列表如下:

public function edit($id) 
{  
    $data = Shops::with('sections')->where('id', '=', $id)->get(); 
    $sectionslst = Sections::lists('section_name','id'); 
    return View::make('admin.shops.edit')->with('data',$data) 
        ->with('sectionslst', $sectionslst); 
} 

,我在我看來,這種方式

{{Form::label('shop_section','الاقسام') }} 
    {{ Form::select('shop_sections[]', $sectionslst, array (1, 2), array ('multiple' => 'multiple'))}} 

//I'm using array(1, 2) just to try how it works, but it doesn't, all items still un-selected :-(
+0

你能告訴我們'$ sectionlst'的轉儲嗎? – lukasgeiter 2015-01-21 10:08:19

+0

我編輯了我的問題。 Plz,再來一次。 @lukasgeiter – 2015-01-21 10:31:53

+0

謝謝,但這不是我的意思。創建一個'var_dump($ sectionlst)',然後在這裏複製粘貼輸出而不是截圖。您還使用了Laravel的確切版本? – lukasgeiter 2015-01-21 10:33:59

回答

2

首先填充數據,你顯然不能使用array(1,2)如果要測試它,你不要你的名單中沒有這些身份證。相反:

{{ Form::select('shop_sections[]', $sectionslst, array(13, 14), array ('multiple' => 'multiple'))}} 

現在,這裏真正的問題是,$data->sections是模型雄辯地收集。但Form::select需要一個鍵數組。你可以通過使用lists()

$data->sections->lists('id') 
+0

非常感謝。它完美的作品。但我需要調整一下 $ data [0] - > sections-> lists('id')。 – 2015-01-21 11:11:56

+0

或者更好一些,使用'first()'而不是'get()'甚至'find($ id)' – lukasgeiter 2015-01-21 11:13:13

+0

我想過了。但恐怕它只返回一個部分。 – 2015-01-21 11:14:56