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 ResourcesUsersUser 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 ResourcesHoliday
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?
2
Answers
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.
This is what I came up with on an enterprise product. (And it works really well.)
I realized that there are only two cases in which I’m using my resource classes:
In this case, I created 2 separate resource classes for every model.
For example, if I wanted to do this for my
User
model, I would create aMinor
and aMajor
resource class underAppHttpResourcesUser
; so it would be like this:The
Minor
class is only used for cases that you need a few properties from theUser
class; and theMajor
class is used for the cases that you need all the important properties.For example, if you send a
GET
request toapi/users
you would expect a list of users withname
,email
andavatar
. You don’t need all the properties in the database, so you would use theMinor
class; but if you send aGET
request toapi/users/1
, you expect to see much more information than before, right? So in this case you want to useMajor
class, which has more properties likename
,email
,avatar
,date_of_birth
,marital_status
andblu_blu_blu
.This way of structuring really helps you when the project is getting bigger. Imagine you have an
Order
class, which has aone to many
relationship with theUser
class; and you want to send aGET
request toapi/orders
; you expect to see a list of orders and their users, right?In this case, you could use the
Minor
class ofUser
model inside theMinor
class ofOrder
model to show their relationship! Right?Using this approach, your data is consistent, and if you want to make a change in one class, that change will be reflected in other classes as well, and you will have created a nice experience in your API.
Note that this is NOT a best practice of any kind; this is what I thought of on my own. I’d be glad if anyone can make this better.
Cheers!