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
Maybe have a look at baum\nestedset package ;)
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.
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community