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

yng's avatar
Level 1

I want Message row from Room instance.

I have a three tables.

Room(id) Recipient(id, room_id, message_id) Message(id)

Room : Recipient -> one to many

Message : Recipient -> one to many

I want Message row from Room instance.

first, I try this code

App\Room::find(1)->recipients()->message()

but didn't work . error code is "no HasMany fun"

how can I get my message row?

0 likes
3 replies
yng's avatar
Level 1

I get data. thank you .

[result]

>>> $test = Room::with('recipients.message')->find(1);
=> App\Room {#4262
     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-26 05:06:31",
     updated_at: "2021-01-26 05:06:31",
     recipients: Illuminate\Database\Eloquent\Collection {#4285
       all: [
         App\Recipient {#4283
           id: 1,
           room_id: 1,
           message_id: 41,
           created_at: "2021-02-09 17:15:16",
           updated_at: "2021-02-09 17:15:16",
           message: App\Message {#4290
             id: 41,
             contents: "test message from 15",
             user_id: 15,
             created_at: "2021-02-09 17:11:59",
             updated_at: "2021-02-09 17:11:59",
           },
         },
       ],
     },
   }

can I get message data without room, recipients ?

yng's avatar
Level 1

I can't under stand well.

Room::with('recipients.messages')->find(1);

<-------------------------------------------------->

select id from room where id = 1 -- A

select message_id from recipients where room_id in (A) -- B

select * from message where message_id in (B)

<------------------------------------------------->

Am I right about what I understand?

Please or to participate in this conversation.