Laravel parent / child relationship on the same model

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Laravel Parent / Child Relationship on the Same Model - A Comprehensive Guide Body:

The Setup And Dummy Data

Firstly, let's set up a model named Category with an appropriate schema that allows for parent and child categories. Here, we have created a hierarchy where each category can be the parent of another category, or it may not have any children. Our example data illustrates two main branches under 'Home', i.e., 'Products' and 'Services'. Moreover, two products ('Product A' and 'Product B') fall under 'Products'.

The Desired Output

We want to achieve a result that shows every category along with its parent and child categories, creating a hierarchical representation. The hierarchy should be displayed in such a way that we can easily identify the parent, children, and grandchildren of any given category. We'll see how to use Laravel's eloquent relationships for this purpose.

The Defined Relationship

We need to define relationships between our model categories using Laravel's eloquent relationships. Since a category can be a parent of another or a child, we defined two relationship methods in the Category model: children and parent. The code to define the relationship should look like this:
public function children()
{
    return $this->hasMany('Category', 'parent_id', 'cat_id');
}

public function parent()
{
    return $this->belongsTo('Category', 'parent_id');
}
The first relationship, 'children', uses hasMany to define that each category may have many child categories. The second relationship, 'parent', is a belongsTo which indicates that each child category must belong to exactly one parent category. Laravel's eloquent relationships help you establish and navigate these connections between objects.

Querying for Child Objects

Now let's access the data using Laravel's query builder. We start by finding our target Category with the findOrFail method:
$category = Category::findOrFail($id);
To obtain all the child categories for this particular category, we use the children() relationship defined in our model:
$children = $category->children();
The code above returns an Eloquent Collection of child Category objects. If there are no children for that specific category, it will return an empty collection. Now we can render these relationships in our view or controller and display the output as a hierarchy. Using this method, you'll have complete control over how the data is displayed while maintaining Laravel's eloquent relationships for querying purposes. This approach not only simplifies your code but also ensures proper maintenance of the parent/child relationships.

Conclusion

Laravel's eloquent relationships provide a straightforward way to manage complex hierarchical data in your application, making it much easier to interact with the database. In this post, we discussed how to use eloquent relationships for one-to-many, many-to-one, and one-to-one parent/child relationships within the same model using an example category hierarchy. You can apply these techniques to any Laravel application that requires managing similar hierarchical structures effectively.