0

So I am currently building out an API and see that laravel has added an API resource which I am assuming is in lieu of something like fractal?

However running into an issue where when I go to return a collection using the XyzCollection resource I do not have the ability to change my DB names? So i end up with the same naming convention I have in my DB..

Plenty of obvious reasons why I don't want to have that be the case (i can go into them if need be) HOWEVER - is there a way to alter those key names?

for example in one of my tables I have a id,uuid,user_uuid Now in fractal I would just transform these fields like so -

'id' => $xyz->uuid, 
'user' => [
    'data' => [
        [
        'user_id' => $xyz->user->uuid,
        'username' => $xyz->user->username,
        ]
    ]
],

How do i do this when all i can do is pass in the collectionin the XyzCollection?

'data' => $this->collection,

and before you say use XyzResource ... When I am returning all(); records like I need to do plenty of times in an API (or paginate) etc etc. I can only do that from XyzCollection!

Thanks in advance

Steve

Citti
  • 423
  • 1
  • 6
  • 23
  • Does your collection derive from a model? If so, you could try extending the model to a new model that is only used by the API. You could then make appended attributes with the names you want, and hide the original attribute names by using the $hidden array on the model. This is a little more elegant than using mapping or loops to rename things programmatically. – eResourcesInc Nov 04 '18 at 16:53

1 Answers1

1

You first need to define a structure for a singular JsonResource object:

(php artisan make:resource Xyz)

public function toArray($request)
{
    return [
        'user_id' => $this->uuid,
        'username' => $this->username,
    ];
}

And then tell your XyzCollection to use the class:

Customizing The Underlying Resource Class

Typically, the $this->collection property of a resource collection is automatically populated with the result of mapping each item of the collection to its singular resource class. The singular resource class is assumed to be the collection's class name without the trailing Collection string.

For example, UserCollection will attempt to map the given user instances into the User resource. To customize this behavior, you may override the $collects property of your resource collection

(From https://laravel.com/docs/5.7/eloquent-resources#concept-overview)

If your ResourceCollection doesn't do anything extra, you might not always need it. Every JsonResource can transform itself into a resource collection with the collection() method, e.g. UserResource::collection($users)

Travis Britz
  • 5,094
  • 2
  • 20
  • 35
  • just getting a chance to look at this - From a glance at what you posted from the docs I think this might have hit the nail on the head - dont know how I missed the $collects override - will report back when I dig into this! – Citti Nov 05 '18 at 04:11
  • ugh I know how I missed the override! just went back to my docs page open and it was on v5.5! must have been the version I came in from through googling! FUNNNNNN!!!!!! – Citti Nov 05 '18 at 04:15
  • Ok all - so clearly version 5.7 of the docs lay it out much nicer :) MY FAULT. So the piece of the puzzle i was missing was that -> "resource collection is automatically populated with the result of mapping each item of the collection to its singular resource class" once I changed the naming so it actually picked it up properly - Its now picking up my collections exactly the way I want to display them! Thanks @Travis – Citti Nov 05 '18 at 04:36