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

yng's avatar
Level 1

pagination with (with function)

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"
                }
            }
        ]
    }
}
0 likes
4 replies
Snapey's avatar

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

yng's avatar
Level 1

yes. I want messages belonging to recipients

how about

$messages = Room::with('recipients.message')->find($request->room_id)->recipients->paginate()

-> BadMethodCallException with message 'Method Illuminate\Database\Eloquent\Collection::paginate does not exist.'

CorvS's avatar

@yng You just want the messages for a specific room, right?

$messages = Message::whereHas('recipient', function($q) use ($request) {
    $q->where('room_id', $request->room_id);
})->paginate(1337);

Assuming you defined the recipient() relationship on your Message model.

yng's avatar
Level 1

wow, I couldn't think about accessing from messages. I just used room model first.

I will try this code . thank you !

Please or to participate in this conversation.