think about your query. it ends with find which means it will only return one result. You cannot paginate its children
sounds like you need to get messages belonging to recipients that belong to room
I used "with" function and I want pagination.
but When I used paginate() after "with" func
It didn't work well.
I just want Message data .
$messages = Room::with('recipients.message')->find($request->room_id);
return response()->json(['messages' => $messages]);
Room Model
public function recipients() {
return $this->hasMany(Recipient::class);
}
Recipient Model
public function message() {
return $this->belongsTo(Message::class);
}
result
{
"messages": {
"id": 1,
"name": "room by Kayli Hand",
"info": "Vel saepe et voluptatum neque omnis in. Nemo modi deserunt quia laudantium at maxime consequatur. Voluptatum et illum magnam magni quis ipsam.",
"prof_pict": "https://t1.daumcdn.net/cfile/tistory/99FAE3475C6FDBC734",
"creator": 16,
"created_at": "2021-01-26T05:06:31.000000Z",
"updated_at": "2021-01-26T05:06:31.000000Z",
"recipients": [
{
"id": 1,
"room_id": 1,
"message_id": 41,
"created_at": "2021-02-09T17:15:16.000000Z",
"updated_at": "2021-02-09T17:15:16.000000Z",
"message": {
"id": 41,
"contents": "test message from 15",
"user_id": 15,
"created_at": "2021-02-09T17:11:59.000000Z",
"updated_at": "2021-02-09T17:11:59.000000Z"
}
},
{
"id": 2,
"room_id": 1,
"message_id": 42,
"created_at": "2021-02-10T16:08:01.000000Z",
"updated_at": "2021-02-10T16:08:01.000000Z",
"message": {
"id": 42,
"contents": "test",
"user_id": 43,
"created_at": "2021-02-10T16:08:01.000000Z",
"updated_at": "2021-02-10T16:08:01.000000Z"
}
},
{
"id": 3,
"room_id": 1,
"message_id": 45,
"created_at": "2021-02-10T16:12:20.000000Z",
"updated_at": "2021-02-10T16:12:20.000000Z",
"message": {
"id": 45,
"contents": "test now1",
"user_id": 43,
"created_at": "2021-02-10T16:12:20.000000Z",
"updated_at": "2021-02-10T16:12:20.000000Z"
}
},
{
"id": 4,
"room_id": 1,
"message_id": 46,
"created_at": "2021-02-10T16:15:02.000000Z",
"updated_at": "2021-02-10T16:15:02.000000Z",
"message": {
"id": 46,
"contents": "test now2",
"user_id": 43,
"created_at": "2021-02-10T16:15:01.000000Z",
"updated_at": "2021-02-10T16:15:01.000000Z"
}
},
{
"id": 5,
"room_id": 1,
"message_id": 47,
"created_at": "2021-02-10T16:15:22.000000Z",
"updated_at": "2021-02-10T16:15:22.000000Z",
"message": {
"id": 47,
"contents": "test now3",
"user_id": 43,
"created_at": "2021-02-10T16:15:22.000000Z",
"updated_at": "2021-02-10T16:15:22.000000Z"
}
},
{
"id": 6,
"room_id": 1,
"message_id": 48,
"created_at": "2021-02-10T16:16:11.000000Z",
"updated_at": "2021-02-10T16:16:11.000000Z",
"message": {
"id": 48,
"contents": "test now3",
"user_id": 43,
"created_at": "2021-02-10T16:16:11.000000Z",
"updated_at": "2021-02-10T16:16:11.000000Z"
}
},
{
"id": 7,
"room_id": 1,
"message_id": 49,
"created_at": "2021-02-10T16:16:53.000000Z",
"updated_at": "2021-02-10T16:16:53.000000Z",
"message": {
"id": 49,
"contents": "test now3",
"user_id": 43,
"created_at": "2021-02-10T16:16:53.000000Z",
"updated_at": "2021-02-10T16:16:53.000000Z"
}
},
{
"id": 8,
"room_id": 1,
"message_id": 50,
"created_at": "2021-02-10T16:17:07.000000Z",
"updated_at": "2021-02-10T16:17:07.000000Z",
"message": {
"id": 50,
"contents": "test now3",
"user_id": 43,
"created_at": "2021-02-10T16:17:06.000000Z",
"updated_at": "2021-02-10T16:17:06.000000Z"
}
},
{
"id": 9,
"room_id": 1,
"message_id": 51,
"created_at": "2021-02-10T16:21:04.000000Z",
"updated_at": "2021-02-10T16:21:04.000000Z",
"message": {
"id": 51,
"contents": "test now3",
"user_id": 43,
"created_at": "2021-02-10T16:21:04.000000Z",
"updated_at": "2021-02-10T16:21:04.000000Z"
}
},
{
"id": 10,
"room_id": 1,
"message_id": 52,
"created_at": "2021-02-10T16:21:19.000000Z",
"updated_at": "2021-02-10T16:21:19.000000Z",
"message": {
"id": 52,
"contents": "하하",
"user_id": 43,
"created_at": "2021-02-10T16:21:19.000000Z",
"updated_at": "2021-02-10T16:21:19.000000Z"
}
}
]
}
}
Please or to participate in this conversation.