How to create a nested-list of categories in Laravel?

后端 未结 6 1661
盖世英雄少女心
盖世英雄少女心 2020-12-08 17:34

How can I create a nested list of categories in Laravel?

I want to create something like this:

  • --- Php
  • ------ Laravel
6条回答
  •  萌比男神i
    2020-12-08 17:53

    This also worked:

    View:

        $traverse = function ($categories) use (&$traverse) {
            foreach ($categories as $category) $traverse($cat->Children);
        };
        $traverse(array ($category));
    

    Model:

    public function Children()
    {
        return $this->hasMany($this, 'parent');
    }
    
    public function Parent()
    {
        return $this->hasOne($this,'id','parent');
    }
    

提交回复
热议问题