Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save agoalofalife/3b39db5d24bcd1322f10aa9875333fb2 to your computer and use it in GitHub Desktop.
Save agoalofalife/3b39db5d24bcd1322f10aa9875333fb2 to your computer and use it in GitHub Desktop.
<?php
trait Tree
{
protected $nameChildAttribute = 'children';
public function getTree(\Illuminate\Support\Collection &$tree, $nameMethod) : array
{
foreach ($tree as &$parent)
{
if ( $parent->child->isEmpty() === false)
{
$parent[$this->nameChildAttribute] = $parent->$nameMethod;
recursive($parent->$nameMethod);
}
}
return $tree->toArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment