讨论数量:
最近用 laravel-nestedset 实现过
- 安装 laravel-nestedset,添加数据库字段,设置模型;
- 资源列表页:关闭分页,这样会一次性显示所有的条目;
- 资源列表页:覆盖 getTableQuery 方法,
parent::getTableQuery()->defaultOrder()->withDepth()
按顺序排列,方便渲染成树; - 模型:增加 indentedName 属性,按照 depth 在开头添加空白字符
// app/Models/Category.php
protected function indentedName(): Attribute
{
return Attribute::make(
get: fn($value, $attributes) =>
Str::of("\u{00A0}")
->repeat(6 * $attributes['depth'] ?? 0) . $attributes['name'],
);
}
最近用 laravel-nestedset 实现过
parent::getTableQuery()->defaultOrder()->withDepth()
按顺序排列,方便渲染成树;