Reputation: 1672
I have:
$collection=Event::all();
$keyed = $collection->mapWithKeys(function ($item) {
return ['title' => $item['name'], 'start' => $item['event_date']];
});
$keyed->all()
is returning only last item in the collection with changed keys:
Array
(
[title] => New Year
[start] => 2018-01-01
)
How do i get all events with changed keys?
Upvotes: 0
Views: 610
Reputation: 1559
The callback of mapWithKeys return an associative array conaining a single key / value pair, so if you just need name and event_date I suggest to use pluck like this:
$collection=Event::pluck('event_date', 'name');
Upvotes: 2