Last active
July 12, 2023 18:48
-
-
Save whoisryosuke/97d6bb9e4f22c2ab02bd867b56a4f6c7 to your computer and use it in GitHub Desktop.
Laravel - API Resources - How to change the collection array that gets returned using the transform method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PageResourceCollection extends ResourceCollection | |
{ | |
public function toArray($request) | |
{ | |
return [ | |
'data' => $this->collection->transform(function($page){ | |
return [ | |
'id' => $page->id, | |
'title' => $page->title, | |
'slug' => $page->slug, | |
]; | |
}), | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi