Eloquent Parent-Child relationship on same model

39,552

Solution 1

You would have to recursively get the children if you have an unknown depth like that.

Another option is to use the nested sets model instead of the adjacency list model. You can use something like baum/baum package for Laravel for nested sets.

"A nested set is a smart way to implement an ordered tree that allows for fast, non-recursive queries." - https://github.com/etrepat/baum

With this package you have methods like getDescendants to get all children and nested children and toHierarchy to get a complete tree hierarchy.

Wikipedia - Nested Set Model

Baum - Nested Set pattern for Laravel's Eloquent ORM

Managing Hierarchical Data in MySQL

Solution 2

You should use with('children') in the children relation and with('parent') in the parent relations.

For your code to be recursive:

public function parent()
{
    return $this->belongsTo('App\CourseModule','parent_id')->where('parent_id',0)->with('parent');
}

public function children()
{
    return $this->hasMany('App\CourseModule','parent_id')->with('children');
}

Note: Make sure your code has some or the other exit conditions otherwise it will end up in a never ending loop.

Solution 3

here is the answer that can help you

I think you you have to do it recursively to retrieve whole tree:

$data = CourseModule::with('child_rec');

Recursive function

This may help you according to your requirement,

public function child()
{
   return $this->hasMany('App\CourseModule', 'parent');
}
public function children_rec()
{
   return $this->child()->with('children_rec');
   // which is equivalent to:
   // return $this->hasMany('App\CourseModule', 'parent')->with('children_rec);
}
// parent
public function parent()
{
   return $this->belongsTo('App\CourseModule','parent');
}

// all ascendants
public function parent_rec()
{
   return $this->parent()->with('parent_rec');
}

Solution 4

your model relationship should like this

     // parent relation
     public function parent(){

        return $this->belongsTo(self::class , 'parent_id');
    }
    //child relation
     public function children()
    {
        return $this->hasMany(self::class ,'parent_id');
    }
    public function ascendings()
    {
        $ascendings = collect();

        $user = $this;

        while($user->parent) {
            $ascendings->push($user->parent);

            if ($user->parent) {
                $user = $user->parent;
            }
        }

        return $ascendings;
    }



    public function descendings()
    {
        $descendings = collect();
        $children = $this->children;

        while ($children->count()) {

            $child = $children->shift();

            $descendings->push($child);

            $children = $children->merge($child->children);

        }
        return $descendings;
    }

Solution 5

Model Function:

public function Children()
{ 
    return $this->hasMany(self::class, 'Parent', 'Id')->with('Children');
} 

Controller Function:

Menu::with("Children")->where(["Parent" => 0])->get(); 
Share:
39,552

Related videos on Youtube

Kiran LM
Author by

Kiran LM

Updated on December 17, 2020

Comments

  • Kiran LM
    Kiran LM over 3 years

    I have a model CourseModule, and each of the items are related to the same model.

    Database Structure:

    enter image description here

    Relation in Model:

        public function parent()
        {
            return $this->belongsTo('App\CourseModule','parent_id')->where('parent_id',0);
        }
    
        public function children()
        {
            return $this->hasMany('App\CourseModule','parent_id');
        }
    

    I tried the following, but it returns only a single level of relation.

    Tried:

    CourseModule::with('children')->get();
    

    I'm trying to create a json output like the following,

    Expected Output:

    [
      {
        "id": "1",
        "parent_id": "0",
        "course_id": "2",
        "name": "Parent",
        "description": "first parent",
        "order_id": "1",
        "created_at": "-0001-11-30 00:00:00",
        "updated_at": "-0001-11-30 00:00:00",
        "children": [
          {
            "id": "2",
            "parent_id": "1",
            "course_id": "2",
            "name": "Child 1",
            "description": "child of parent",
            "order_id": "2",
            "created_at": "-0001-11-30 00:00:00",
            "updated_at": "-0001-11-30 00:00:00",
            "children": [
              {
                "id": "3",
                "parent_id": "2",
                "course_id": "2",
                "name": "Child2",
                "description": "child of child1",
                "order_id": "2",
                "created_at": "-0001-11-30 00:00:00",
                "updated_at": "-0001-11-30 00:00:00",
                "children": [
                  {
                    "id": "4",
                    "parent_id": "3",
                    "course_id": "2",
                    "name": "Child 3",
                    "description": "child of child 2",
                    "order_id": "2",
                    "created_at": "-0001-11-30 00:00:00",
                    "updated_at": "-0001-11-30 00:00:00",
                    "children": []
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
    

    I don't understand how to get the inner child objects.

  • Kiran LM
    Kiran LM over 8 years
    Hi, CourseModule::whereId($id)->first()->getDescendantsAndSelf()‌​->toHierarchy() returns only single node, should i have to make any changes to the model, i'm working with a single table as shown in top
  • Shahbaz Ahmed
    Shahbaz Ahmed over 6 years
    It works! In first collection I have a chained tree but in same children are repeated as parent collection. I don't want the children again as root collection. Is there any solution or am I doing wrong?
  • Shahbaz Ahmed
    Shahbaz Ahmed over 6 years
    Found the solution $data = CourseModule::with('children')->where('parent_id', 0)
  • Rashed Hasan
    Rashed Hasan almost 6 years
    I am trying to display the results as the hierarchy, but could not. I tried something like this @foreach ($accountHeads as $accountHead) @foreach ($accountHead->children as $children) <option value="{{ $children->id }}">{{ $children->name }}</option> @endforeach @endforeach . But it showing only those, which has parent_id = 1 .
  • vezunchik
    vezunchik about 5 years
    Please, can you extend your answer with more detailed explanation? This will be very useful for understanding. Thank you!