Eloquent Parent-Child relationship on same model
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Nesting Child Relations Using Laravel Eloquent Models
When building applications in Laravel using Eloquent models, working with parent-child relationships can be a bit challenging, especially when they are on the same model. However, achieving this is not impossible with the help of nested relations and correct database design. In this blog post, we'll explore how to handle these complexities by creating a tree-like structure of nesting child objects within their parents. Firstly, let us assume you have a model calledCourseModule, which has multiple levels of parent-child relationships within itself. The following database structure and relations should be set up:
Database Structure:

CourseModule model, which is the parent, has a column called 'parent_id' that stores the ID of its parent. As shown in the diagram above, there are three levels of nesting:
1. Root courses with an id=0 (represented by the horizontal line at the top).
2. First-level children connected to their respective root courses.
3. Second-level children connected to first-level courses.
4. And so on, until no children are present in any level.
To achieve this structure and relations in your Laravel application, create a corresponding model with the necessary fields and relationships:
Model Relations for Each Level:
1. At the root level (parent_id=0), you'll have only root courses:public function parent() { return $this->belongsTo('App\CourseModule','parent_id')->where('parent_id',0); }
2. For the first child level, create a relation for each first-level child to its root:
public function children() { return $this->hasMany('App\CourseModule','parent_id'); }
3. Finally, at the second child level and beyond, create relations between the respective parent and its children:
public function children() { return $this->hasMany('App\CourseModule','parent_id'); }
Now that you have set up your model relationships, let's move on to loading the nested data. In your controller, you can use Eloquent relationships to load and retrieve all levels of parent-child relationships:
Controller Code:
$courseModules = CourseModule::with('children')->get();
This will load the root courses (level 1) along with their child courses. However, to get the nested children (levels 2 and beyond), you need a recursive function:
Recursive Function for Nested Child Relations:
```php public function allChildModules($courseModuleId, $depth = 0) { // Base case for level 1 children (if any exists) if ($depth === 0) { return CourseModule::whereIn('parent_id', CourseModule::where('id', '=', $courseModuleId)->pluck('parent_id'))->get(); } // Get first-level children of the given parent $firstLevelChildren = CourseModule::where(['parent_id' => $courseModuleId, 'depth' => 1])->with('allChildModules')->get(); // Recursive call for the next levels (if needed) foreach ($firstLevelChildren as $child) { $recursiveChildren = CourseModule::where(['parent_id' => $child->id, 'depth' => 2])->with('allChildModules')->get(); if (!empty($recursiveChildren)) { $child->addRecursiveChildren($recursiveChildren); } } return $firstLevelChildren; } ``` This function recursively loads the first-level children and their nested children (if present). To use this, you can update your controller code:Modified Controller Code:
$courseModules = CourseModule::with('children')->get();
foreach ($courseModules as $root) {
$nestedChildren = allChildModules($root->id);
foreach ($nestedChildren as $child) {
$recursiveChildren = allChildModules($child->id, 2); // Increase depth to handle level 2 and beyond children
if (!empty($recursiveChildren)) {
$child->addRecursiveChildren($recursiveChildren);
}
}
}
This example demonstrates how you can retrieve the nested child objects from your database using Laravel Eloquent models and recursion. The provided controller code loops through each root course (level 1) and calls the allChildModules() function to load their children and grandchildren. This approach helps in creating a tree of CourseModule objects with nested relationships. To learn more about Laravel Eloquent, visit our blog post on [Eloquent Relationships](https://laravelcompany.com/blog/eloquent-relations).