Question

I'm new to Laravel 4 and some concepts it uses so go easy on me.. I'm developing a web store and I have a problem getting all the articles from all descendants in my catalogue tree.

My tree structure is stored in the catalogues table using nested-sets (Cartalyst):

Category 1
--Sub-category 1.1
----Sub-category 1.1.1
----Sub-category 1.1.2
--Sub-category 1.2
Category 2...

Then I have a table for articles and a pivot table article_catalogue. So I paired the Sub-category 1.1.1 with 2 articles and Sub-category 1.1.2 with 3 articles using the pivot table. Now in my models I defined the many-many relationship so I could use Eloquent ORM.

Now comes my problem, when a user clicks on Sub-category 1.1 or even on Category 1 I would like to show all the articles in the selected node (if any) and from all the descendants of the selected tree node excluding duplicates and have paging.

I know I could pair the articles with all the nodes in the pivot table but is there any way to do this as described using Eloquent and nested-sets from Cartalyst?

I'm open to alternative solutions/suggestions...

Was it helpful?

Solution

I ended up using query builder and it works with this:

$articles = DB::table('articles')
->join('article_catalog', 'article_catalog.article_id', '=', 'articles.id')
->join('catalogs', 'catalogs.id', '=', 'article_catalog.catalog_id')
->whereBetween('catalogs.lft', array($activeItem->lft, $activeItem->rgt))
->select('articles.*')->paginate(9);

Thank you crynobone for all your help in the Laravel IRC chat room! If someone comes up with a better solution let me know!:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top