Cyvelnet / laravel5-fractal

A simple fractal service provider and transformer generator for laravel >=5
MIT License
79 stars 21 forks source link

Do you have to return an array and specify every item of an object? #19

Closed abodnar closed 8 years ago

abodnar commented 8 years ago

In the transform function of my transformer, do I have to return an array? And do I have to set every single key in the array for what I want returned?

Or am I able to just change the points of data on the object and return the object?

Something like this:

`class Transformer extends TransformerAbstract { public function transform($obj) { $obj->id = md5($obj->id);

  return $obj;

} } `

Also, what do you typically put in $availableIncludes and $defaultIncludes?

Cyvelnet commented 8 years ago

yes, you have to set the key value pair,

the $availableIncludes is for the sub relationship which is available under the current object.

eg. http://api.example.com/users/1?include=order

by referring to the sample url above, if you have set $availableInclude = ['order']; ,

or

Fractal::includes('order')->collection($users, new UserTransformer());

and having a method includeOrder(), the includeOrder will be loaded and injected into your api response.

and $defaultIncludes will always be call and injected on every single transformation.