I have the following collection in Laravel:
["TheNumbers":[{"episodeID":16818,"episodeNumber":100,"created_at":null,"updated_at":null},{"episodeID":16818,"episodeNumber":210,"created_at":"2017-02-20 21:30:38","updated_at":"2017-02-20 21:30:38"}]
If I run the following code:
$TheEpisode->TheNumbers->pluck('episodeNumber');
I will get the following result:
[100,210]
I would like to keep the keys for each number, how can I achieve this?
EDIT: EXPECTED RESULT:
This is my expected result:
[{"episodeNumber":100},{"episodeNumber":210}]
PHILIS PETERS (improved)
TheEpisode->TheNumbers->reduce(function ($result, $episode) { $episodeData = (collect())->put('episodeNumber', $episode['episodeNumber']); $result[] = $episodeData; return $result; }));
Advertisement
Answer
Try something like this, it should work using map
…
return $TheEpisode->TheNumbers->map(function ($episode) { return ['episodeNumber' => $episode['episodeNumber']]; });