Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Hi Nate,

I am currently using a hybrid of Nested Set Model and Adjacency List Model (parent_id), it works really well, although updates are pretty difficult when you're changing a node's parent, and get progressively slower as the size of your record-set increases, this model should be perfect for categories.

There are a couple of libraries I've seen for this, one specifically for Laravel, so if you're using Eloquent, it should be really easy to implement.

https://github.com/lazychaser/laravel4-nestedset

https://github.com/etrepat/baum/tree/master/src/Baum

I'm not using Eloquent, so have had to implement the Create/Update logic myself, if you need to go that way, post back and I'll help you out if I can.

Regards,

Andy

Last updated 2 years ago.
0

I have been looking for this the latest couple of days. This are my thoughts so far:

Nested sets. I looked at some of theese (baum) but I dont like the idea of having some root in the db that doesnt do anything but hold the hierarchy. When I tried multiple roots it was hard getting the whole tree. Maybe I'm missing something?

I also tried a selfrefencing Eloquent parent/children relationship, and it worked but then you dont know the depth, and I'm not sure how efficient it is.

Another option for me was using PostgreSQL which has some nice functions (tablefunc or the ltree datatype) for hierarchy. However I had a hard time getting the operators and functions work in Eloquent or Fluent. It complained that <@ wasnt a valid operator and such.

At the moment I'm trying out using a closure table with this package: https://github.com/franzose/ClosureTable and caching the result. I'm not done testing this yet... I hope it will work out.

I asked about this on laracasts.com the other day: https://laracasts.com/forum/1299-tree-structure-is-there-a-shortcut

I'm surprised how little information there are on this online and how complex it seems to accomplish in Laravel, where everything else seems so simple ;) After all, this must be a really common task in many applications?

Last updated 2 years ago.
0

Thank you both for the replies.

@Andy, I'm going to test out the lazychaser L4 Nested Set. It looks like what I'm looking for!

@mattiaswirft, Yeah I thought it was a common thing that people needed. In most CMS's and shopping carts that I've messed with, it seems easy to add an infinite amount of children categories - but when I start planning it into my own application, I suddenly am at a loss on the logistics of it. Seems most tutorials and explanations out there are geared toward CRUD and Testing. This is definitely on the backburner.

Last updated 2 years ago.
0
Last updated 2 years ago.
0

Thanks zenry! That looks like a great option too. Documentation looks really nice.

Last updated 2 years ago.
0

NateLedet said:

Thanks zenry! That looks like a great option too. Documentation looks really nice.

I've released a v.2.0-beta of https://github.com/lazychaser/laravel4-nestedset, check it out! I've rewritten docs and got rid of some limitations.

Last updated 2 years ago.
0

pros/cons? baum / laravel4-nestedset?

Last updated 2 years ago.
0

Is there some basic tutorial for baum somewhere ?

Last updated 2 years ago.
0

I'm looking for a tutorial too, I have 4 levels of categories but I don't really know how to show children items in parents or make proper breadcrumbs

Did you find something??

Last updated 2 years ago.
0

Here they have an example of this. But still confusing to me How to apply this in real life project.

Is there any one who can explain more.

https://github.com/etrepat/baum/wiki/Example:-Products-and-categories

Last updated 9 years ago.
0

If anyone's still interested in this here's an example implementation:

https://gist.github.com/patrickisgreat/a533a779dd1d2c01e76d58b06d3fc833

0

Sign in to participate in this thread!

Eventy

Your banner here too?

NateLedet nateledet Joined 8 Apr 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.

© 2024 Laravel.io - All rights reserved.