这是我想要生成多级导航的类别表迁移脚本。
注意:我使用“父”列生成多级菜单。
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('description');
$table->string('icon');
$table->tinyInteger('active')->default(1);
$table->integer('sort')->default(0);
$table->integer('views')->default(0);
$table->integer('parent')->default(0);
$table->timestamps();
$table->softDeletes();
});
使用最小循环生成主菜单,子菜单和子子菜单树的最佳方法是什么,在视图中使用会很好并且有用吗?
我想要实现的结果应该是以下,它是三级深度,用于生成3级导航。
Array
(
[0] => Array
(
[id] => 1
[title] => Accessories
[parent] => 0
[sub_cats] => Array
(
[0] => Array
(
[id] => 29
[title] => Kids
[parent] => 1
[sub_cats] => Array
(
[0] => Array
(
[id] => 41
[title] => Shirts
[parent] => 29
)
[1] => Array
(
[id] => 42
[title] => Toys
[parent] => 29
)
)
)
[1] => Array
(
[id] => 30
[title] => Men
[parent] => 1
)
[2] => Array
(
[id] => 31
[title] => Women
[parent] => 1
)
)
)
[1] => Array
(
[id] => 5
[title] => Cakes
[parent] => 0
)
)