public static function toTree(array $elements = [], $parentId = 0)
{
$branch = [];
if (empty($elements)) {
$elements = static::with('roles')->orderByRaw('`order` = 0,`order`')->get()->toArray();
}
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = static::toTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}