Steve
Steve

Reputation: 1672

Getting all items in the collection mapWithKeys

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

Answers (1)

MohamedSabil83
MohamedSabil83

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

Related Questions