尝试将 collectino 传递给视图中的表单选择。prepend 方法是重新索引集合,我丢失了正确的公司 ID。
$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"
* ]
*/
我现在正在前面的方法中查看集合对象,这里是:
public function prepend($value, $key = null)
{
$this->items = Arr::prepend($this->items, $value, $key);
return $this;
}
好的,
我很快就找到了解决方案。通过为第二个参数传递一个键,我使用的是 0,该方法将保持原始键。
$companies->prepend('Select a company', 0);
return $companies;
'*
* {
* "0": "Select a company",
* "3": "Test 123 ",
* "4": "wer"
* }
*'