9 months ago

Tree of categories

Posted 9 months ago by sampaioPT

Hello guys,

I have a php application developed in laravel and I want to develop a tree of categories (with 3 levels of categories or less) but I don't know how to do this... For example: a character (Son goku) could have this tree of categories (with 2 levels) -> Warrior/Saiyajin that Warrior category is the "father" of Saiyajin. Although other character (Yamcha) could have this tree of categories (with 3 levels) -> Warrior/Terrestrial/Always die. Bare in mind that I could have many father's categories (not only Warrior) and a character only have one tree of categories. I already created a categories table with 3 fields:


And create a Category model:

class Category extends Model
protected $table = 'categories';

    protected $fillable =

This is the function to relate Character to the Category Model:

class Character extends Model
  public function category()

        return $this->belongsTo('App\Entities\Category');

I have 3 problems:

-> I dont know how to create the relations into Category Model;

-> I don't know how to print the categories tree of one character (Warrior/Terrestrial/Always die);

-> And when I am going to create a new characeter I need to return the tree of all categories.

Any help will be appreciated.

(Maybe is better to gather all the 7 dragon balls and call the Shenron to help me in this situation :) )

Please sign in or create an account to participate in this conversation.