Search code examples
laravellaravel-4

Category tree in a laravel


I am building a product catalogue.

I have following models: Product, Category. Each product can have many categories and each category as well.

Product model:

public function categories() {
  return $this->belongsToMany('Category');
}

Category model:

public function products() {
  return $this->belongsToMany('Product');
}

How can i make category tree without using 3rd party packages like Baum or Nested Sets (They seem too complicated for my task).

Something like that:

Category1
--subcategory (level1)
---subcategory (level2)
Category2
--subcategory (level1)
---subcategory (level2)

Each category has following fields in db:

id
title
parent_id

How can i set category depth?


Solution

  • Ended up without using any 3rd party packages. Used native Eloquent for that.