Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Last active July 12, 2023 18:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save whoisryosuke/97d6bb9e4f22c2ab02bd867b56a4f6c7 to your computer and use it in GitHub Desktop.
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
<?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,
];
}),
];
}
}
@bayramgeldiyevichh
Copy link

Hi

@pablohernandez1983
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment