tobyzerner / json-api-server

A JSON:API server implementation in PHP.
https://tobyzerner.github.io/json-api-server/
MIT License
63 stars 21 forks source link

feat: allow constraining `ToMany` query #103

Closed SychO9 closed 6 months ago

SychO9 commented 7 months ago

A better alternative to #102, generally allowing to constrain the relationship query during eager load is more flexible for use cases like the one outlined in the stale PR.

tobyzerner commented 6 months ago

This sounds good. I think I'd like to call this scope to be consistent with resources. Any reason to not allow it on ToOne relationships too? (Move to the base Relationship class)

tobyzerner commented 6 months ago

Actually, just remembered that scope is an Eloquent-specific behaviour so I'm not sure about including this functionality in the framework-agnostic Relationship classes. I'll have a think about an alternative approach, maybe like making those classes macroable or something.

tobyzerner commented 6 months ago

OK, I think the best approach will be to subclass the ToOne and ToMany classes under a new Tobyz\JsonApiServer\Laravel\Field namespace, and both of them can use a trait which adds a scope method. This way the concept of scoping remains entirely within the Laravel namespace.

SychO9 commented 6 months ago

Done 👍🏼

tobyzerner commented 6 months ago

Thank you!