zer*_*nes 6 php oop collections laravel
试图将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"
* ]
*/
Run Code Online (Sandbox Code Playgroud)
我现在在prepend方法的Collection对象中查找,它是:
public function prepend($value, $key = null)
{
$this->items = Arr::prepend($this->items, $value, $key);
return $this;
}
Run Code Online (Sandbox Code Playgroud)
好吧,我很快找到了解决方案。通过为第二个参数传递键,我使用的是0,该方法将保留原始键。
$companies->prepend('Select a company', 0);
return $companies;
\*
* {
* "0": "Select a company",
* "3": "Test 123 ",
* "4": "wer"
* }
*\
Run Code Online (Sandbox Code Playgroud)