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

murilo's avatar
Level 10

Send Message to Users ( 2 kind of users ), Polymorphic Relations

Hello Guys , I have a question , I dont know if some one can help me here , I have a project of a school , it has 2 kinds of Users - Teacher and Students .

I am making a message system that they will be able to communicate with them self . I have 2 tables for users - user_teacher and user_student . So when I send a message , I tell the database , that I am sending a message to Teacher (user_teacher Table ) to Student ( user_student ) .

Ok , That wold be simple , but the the problem is , if I want to send a message for Student ( user_student table ) to Student ( user_student table ) or Student to Teacher , Teacher to Teacher ... . So I will need a Pivot Table called Message_user that will comunicate with Message Table and The Users Table , that will tell the -

User_Sending_id  
User_Sending_Model (TEACHER OR STUDENT ? )

User_receiving _id
User_receiving _Model  (TEACHER OR STUDENT ? )

So I will need a Polymorphic Relations for doing that , But I have never done Polymorphic Relations setting 2 models in the same table , All the examples that I have seen , is just setting one model in one table . Does have some one that have donne something like this ? Is that a good approach ?

Thanks

0 likes
0 replies

Please or to participate in this conversation.