Support the ongoing development of Laravel.io →
Database
Last updated 2 years ago.
0

please anyone suggest me for n level hierarchy relationship.

Last updated 2 years ago.
0

I don't understand the problem. For each survey you just have to use $survey->children to retrieve its children, then you can use $child->children to retrieve the children of a child, etc...

Last updated 2 years ago.
0

Thanks a lot pmall !!!! but i want a relationship in below format.... [{ "id" : 1, "name" : "Geneal", "type" : "group", "children" : [{ "id" : 2, "name" : "What is..?", "type" : "question", "children" : [{ "id" : 3, "name" : "aa", "type" : "answer" }, { "id" : 4, "name" : "bb", "type" : "answer" }, { "id" : 5, "name" : "cc", "type" : "answer" }]}, { "id" : 6, "name" : "How is..?", "type" : "question", "children" : [{ "id" : 7, "name" : "ba", "type" : "answer" }, { "id" : 8, "name" : "bb", "type" : "answer" }] }] ... and so on }] i used $group = Survey::with('parent' ,'children')->get(); from this how we can achieve n level relationship... and i make parent and child functions in Survey model. i can get only 2nd level relationship. so pls suggest me..

Thanks HARSH SHAH

Last updated 2 years ago.
0

You can eager load many levels of relationship by using a dot. I dont know if it works well with self referencing relationships, never tried, but you may try Survey::with('parent', 'children.children')->get();

Last updated 2 years ago.
0

Thanks a lot pmall !!!!!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.