I wonder what are the best practices that the community have about the architecture of Laravel's Resources.
I started using it to avoid sending unnecessary data to certain routes of the API.
I structured it the way my API routes looks like, with specific namespaces corresponding to the routes.
However, doing this faced me to a conceptual problem which is that it forces me to duplicates a lot of Resources that often returns the same data.
For example, if I get a route users/user returning a Resources\Users\User UserResource that would return :
return [
'id' => $this->id,
'name' => $this->name,
'firstname' => $this->firstname,
'fullName' => $this->fullName(),
];
And then create another route named holiday/user, I would create the same resource under namespace Resources\Holiday\ or something like that.
Should I create some global folder for example Users in which I would have different representations of a User, and then pick them depending on my route's use case?
But doing this might involve that I sometimes got data I won't need.
I wonder what are the community's best practices on this subject?
What I like to do is organize my resources by model and route action. For example, for a User model, I have the following:
If I need a specific resource for a relation like
$holiday->user, I simply add it to the holiday resources:These organized resource files help me maintain a clear and structured approach to handling different model actions and relationships.