支持 tree 动态切换 pk 主键名称

This commit is contained in:
JaguarJack
2021-05-25 08:28:19 +08:00
parent 3bad548d10
commit 95c6059aa1
2 changed files with 36 additions and 4 deletions

View File

@@ -22,7 +22,13 @@ class CatchModelCollection extends Collection
*/
public function toTree($pid = 0, $pidField = 'parent_id', $children = 'children'): array
{
return Tree::done($this->toArray(), $pid, $pidField, $children);
$pk = 'id';
if ($this->count()) {
$pk = $this->first()->getPk();
}
return Tree::setPk($pk)->done($this->toArray(), $pid, $pidField, $children);
}