-
-
Save FragsterAt/9d16e30b508c305b1ae6921e4bd33c63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
... | |
public static function tree() | |
{ | |
function makeTree(&$sectionBranch, $sectionArray) | |
{ | |
foreach ($sectionBranch as $k => $sectionLeave) { | |
$sectionLeave->children = array_values(array_filter($sectionArray, function ($item) use ($sectionLeave) { | |
return $item->parent_id == $sectionLeave->id; | |
})); | |
makeTree($sectionLeave->children, $sectionArray); | |
$sectionBranch[$k] = $sectionLeave; | |
} | |
}; | |
$sectionArray = array_map(function ($section) { | |
return (object) $section; | |
}, self::with(['items' => function ($query) { | |
$query->select('id', 'section_id'); | |
}, 'recomendation'])->ordered()->toArray()); | |
$sectionTree = [(object) ['id' => null, 'caption' => 'root']]; | |
makeTree($sectionTree, $sectionArray); | |
return $sectionTree[0]->children; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment