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

Sharmigeorge's avatar

one-to-many-to-one relationship

There are 3 tables. table1 and table2 are of one to many relationship. table2 and table3 are of many to one relationship. I want to get the corresponding table3 data for the table1 row using 'with'.

I have connected (table1 and table2) and (table2 and table3) in the model. I tried accessing with the function name of the relationship. But getting function undefined error.

The same method works fine for one-to-one-to-one.

Please help me to solve this.

0 likes
5 replies
lewis4u's avatar

Try to describe your problem with real model names and what are you trying to achieve. For example first table is probably the users table...and a User has something that is in your table2....so try to build up a story and write it here.

Sharmigeorge's avatar

I am developing something like google forms as a feature. And this form has multiple section. Lets say the sections are basic_details, contact_details, education_details of a person.

Each section has multiple fields. For instance, basic_details has fields like, name, age, DOB, contact_details has phone, email, address etc. Each fields are connected to a particular datatype. say name to string, age to integer etc. from the available datatypes from defined_datatypes table.

So, table1 = form_sections, table2 = section_fields(has foreign key to form_section and defined_datatypes ), table3 = defined_datatypes

When I call the complete form get API, I want the form details with the form_sections, section_fields and the corresponding datatypes of every fields.

jaewun's avatar

You're going to have to provide real code examples in order for us to help you.

Please provide your migrations and how you have defined the relationships in your models.

1 like
Sharmigeorge's avatar

Hi @fahad , I have given form_section hasMany form_fields and form_fields belongsTo defined_datatypes. When I call them separately, it works. But together it doesn't.

Please or to participate in this conversation.