flugg / laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal with Laravel's elegancy.
MIT License
865 stars 86 forks source link

Conditional attributes #109

Open flugg opened 6 years ago

flugg commented 6 years ago

One of the distinct features of Laravel 5.5 API resources is the ability to use conditional attributes: https://laravel.com/docs/5.5/eloquent-resources#conditional-attributes.

For simple when and mergeWhen this is simply to avoid if-clauses and makes the code more readable. However, the whenLoaded provides additional functionality. There's also a whenPivotLoaded, but I believe a whenIncludedBy (or whenLoadedBy to be consistent?) can be a nice addition as you can add data only when a relation has been included by a specific transformer:

'foo' => $this->whenIncludedBy(FooTransformer::class, function ($foo) {
    //
},

You will also get access to the parent data.

I think the best way to implement this is to run an array_walk_recursive on the transformed result and replace any placeholder classes with the actual data. It should be looked into how much this affects the performance though.

Hesesses commented 5 years ago

Do you have any estimates when this might be implemented?

flugg commented 5 years ago

Not really, but a bit bummed I haven't had much time to develop this package further the past year. It's in a good state with few bugs, but there are a few of these features I would love to get realised. I'll try my best to get some quality open source time during the summer :)