Spartaque07's avatar

Resource API

Hi all. I want to create api for chat application. Every chat has users and messages relation. I wanna create Resource API for GET /chat/ that will return all chats with auth()->user(). But i wanna have pagination for this chats. And this chat has relation that call as messages, and i wanna have pagination for messages too. Like a inner pagination. Structure of this will be the next:

chats: //paginated [ users: array, messages: array //paginated ]

How i can do this using Laravel Resource API? Help me pls)

0 likes
2 replies
martinbean's avatar

It’s not really going to be possible.

If you have a route that returns 10 conversations for the authenticated user, how are you expecting to paginate the messages for the first conversation?

Instead, have a route that returns conversations for a user:

Route::get('conversations', 'ConversationController@index');

And a route that returns the messages for a given conversations:

Route::get('conversations/{conversation}/messages', 'ConversationMessageController@index');
Spartaque07's avatar

Thanks for your reply. I can create another route to fetch paginated messages, but i want to attouch this paginated route to to conversation paginate. Like: Conversations// paginated [ { ..., Messages // paginated with another route [] } ]

I need to grab 10 conversations with 10 messages for all conversations. After that lazy load messages using paginator for messages)

Please or to participate in this conversation.