2016-04-27 34 views
1

試圖將collectino傳遞給窗體,在視圖中選擇。 prepend方法正在重新編制索引,並且我失去了正確的公司ID。如何將項目添加到laravel列表集合而不重新索引?

$companies = Company::lists('name','id'); 
return $companies; 

/* 
* { 
*  "3": "Test 123 ", 
*  "4": "wer" 
* } 
*/ 

$companies->prepend('Select a company'); 
return $companies; 

/* 
* [ 
*  "Select a company", 
*  "Test 123 ", 
*  "wer" 
* ] 
*/ 

我期待中的前置方法Collection對象,現在,這裏是:

public function prepend($value, $key = null) 
{ 
    $this->items = Arr::prepend($this->items, $value, $key); 

    return $this; 
} 

回答

0

好,我很快就找到了解決方案。通過傳遞第二個參數的鍵,我使用0,該方法將保持原始鍵。

$companies->prepend('Select a company', 0); 
return $companies; 

\* 
    * { 
    *  "0": "Select a company", 
    *  "3": "Test 123 ", 
    *  "4": "wer" 
    * } 
    *\ 
相關問題