Support the ongoing development of Laravel.io →
Database Architecture

I'm building an app with lots of this kind of data, e.g. jobs. Most have a parent so I've started creating models with parent_id.

id | job_name         | parent_id
1  | super manager    | 0
2  | manager          | 1
3  | employee         | 2

Are there better ways to do this? I know about the left|right pattern but I think it's overkill for this situation.

Also, can eloquent relationships handle this? As in:

public function parent($parent_id)
{
    return $this->belongsTo('modelName')->where('parent_id', $parent_id); 
}

Thanks and regards,

Mei

Last updated 3 years ago.
0

Maybe have a look at baum\nestedset package ;)

Last updated 3 years ago.
0

Here's an excellent article describing various hierarchical techniques, particularly when using a relational database such as MySQL. Regardless of the direction you take, read this article; it does a great job of explaining each technique with context.

MANAGING HIERARCHICAL DATA IN MYSQL - Mike Hillyer

Last updated 3 years ago.
0

Thanks for the replies.

As I said, I think that nested sets are a bit overkill, but it's always good to look over someone else's code.

Mei

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

meigwilym meigwilym Joined 5 Feb 2014

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.

© 2025 Laravel.io - All rights reserved.