Skip to content

Instantly share code, notes, and snippets.

@FragsterAt
Last active February 5, 2020 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FragsterAt/9d16e30b508c305b1ae6921e4bd33c63 to your computer and use it in GitHub Desktop.
Save FragsterAt/9d16e30b508c305b1ae6921e4bd33c63 to your computer and use it in GitHub Desktop.
<?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