I have this code where I try to grab all auth user categories:
$cats = Auth::user()->cats()->lists('title','id');
and I want to add new data to $cats so I write:
$cats->push(['5','BMW']);
but I got:
Collection {#459 ▼
#items: array:2 [▼
9 => "asd"
10 => array:2 [▼
0 => "5"
1 => "BMW"
]
]
}
How I to change my code to get this result:
Collection {#459 ▼
#items: array:2 [▼
9 => "asd"
5 => "BMW"
]
}
So how I can add the array to this collection? p.s. I need this format because I use select2 jquery plugin
You can use the collection like an array:
$cats[5] = 'BMW';
I played with it - and it's ugly, but here's... something...
`$item = (object) ['id' => 5, 'val' => 'cat']`
{#925 +"id": 5, +"val": "cat",}
`collect([$item])`
{#924 all: [ {#925 +"id": 5, +"val": "cat", },],}
`collect([$item])->keyBy('id')->transform(
function($obj){ return $obj->val;
})`
{#907 all: [5 => "cat",],}
So I think using keyBy() and transform() might get you where you're wanting to go.