
コレクションデータを加工するときに使えるよ🐱
こんな感じだよ
$collection = collect([4, 5, 6]);
$items = $collection->map(function ($item, $key) {
return $item * 2;
});
$items->all();
// [8, 10, 12]
ここでコレクションを1つずつ加工しています
return $item * 2;
実装例
const LANGUAGE_CODE = 'jpn';
$book_contents = $this->i_c->searchWithSolds($book_ids, [
'is_publish' => $this->getIsPublish(),
'sold' => true
])
$book_contents->map(function ($item) {
$item->image = json_decode($item->image, true);
return $item;
});
$response_data = [
'items' => $this->searchByLanguage($book_contents, self::LANGUAGE_CODE)
]
return $response_data;
コレクションのimageプロパティをJSON型から配列型に加工しています。
$book_contents->map(function ($item) {
$item->image = json_decode($item->image, true);
return $item;
});

