Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shafqat_ali's avatar

Multiple Api resources for same model

Is there any way where I can create multiple api resources for same model.

First resource is properly reflecting the API resource Keys but second resource is not at all reflecting the API keys, it is showing the API keys of first API resource.

Is there anything that I am missing, I want to create different api resources for same model.

0 likes
4 replies
Sinnbeck's avatar

Sure. Just create more than one?

return new UserResource(User::findOrFail($id));
return new UserFoobarResource(User::findOrFail($id));


return UserResource::collection(User::all());
return UserFoobarResource::collection(User::all());
1 like
Shafqat_ali's avatar

@Sinnbeck Thank you so much for replying, I performed the exact step you previously but it didn’t work.

Finally, I figured out that I was so lame that I haven’t write return json->response(new UserResource(User::findorFail($id)));

It turns out if you don’t write json response resource won’t return the keys that you have created.

It was a silly mistake from my side. Anyways once again thank you so much for your reply.

Shafqat_ali's avatar

@Sinnbeck it was xhr request. Now it’s working perfectly as expected. Once again thank you so much for your reply.

Please or to participate in this conversation.