Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonResource #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

JsonResource #1

wants to merge 1 commit into from

Conversation

radmen
Copy link
Collaborator

@radmen radmen commented May 3, 2021

The problem

Laravel API Resource doesn't handle wrapping model relations in their respective API resources

For example: the application defines PostResource & TagResource. The controller fetches posts with their tags and returns PostResource collection. The API resource will convert the model to an array using Model#toArray() method which casts all relations in the same way.

Although there's TagResource, it won't be used.

Proposed solution

Add base JsonResource class that will pipe all related models through resource() factory. This will ensure that each related model will be converted to an array using its API Resource.

TODO

  • use JsonRecourse as a fallback resource in the factory
  • update README

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request 👷 in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant