Trying to pass a collectino to a form select in the view. The prepend method is re-indexing the collection and I'm losing the correct company ids.
$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"
* ]
*/
I'm looking in the Collection object at the prepend method now, here it is:
public function prepend($value, $key = null)
{
$this->items = Arr::prepend($this->items, $value, $key);
return $this;
}
Ok I quickly found a solution. By passing a key for the second argument, I'm using 0, the method will maintain the original keys.
$companies->prepend('Select a company', 0);
return $companies;
\*
* {
* "0": "Select a company",
* "3": "Test 123 ",
* "4": "wer"
* }
*\
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With