laravel-responder icon indicating copy to clipboard operation
laravel-responder copied to clipboard

Conditional attributes

Open flugg opened this issue 7 years ago • 2 comments

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.

flugg avatar Feb 07 '18 10:02 flugg

Do you have any estimates when this might be implemented?

Hesesses avatar Jun 12 '19 13:06 Hesesses

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 :)

flugg avatar Jun 14 '19 07:06 flugg