please anyone suggest me for n level hierarchy relationship.
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...
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
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();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community