I'm building an API using Laravel where I need to use multiple One To One relationships. For nested resource where I use One to Many I defined the routes like this:
/api/scenarios/1/users --> return all users from a scenarios
/api/scenarios/1/users/1 --> return only the user with the id 1
How the One to One Api routes should be defined? Does it makes sense to provide the nested resource id? Example:
/api/scenarios/1/user/1
Or I can define it without the id:
/api/scenarios/1/user
Thanks!
It really depends. It doesn’t make sense to have a /api/scenarios/1/user/1 resource if you also have a users resource where you can get the same user via a URI like /api/user/1, because now you have two routes to maintain that return the same resource, and the URI for the resource is now ambiguous.
Instead of thinking as these things as “nested” resources, you could instead think of them as filterable resources. You have scenarios. You have users. If you want to find users belonging to a particular scenario, then you could simply filter the users resource instead of creating an ambiguous “sub-resource”: /api/users?filter[scenario]=1