public function renderAsArray($data = false, $parent = 0)
{
$args = $this->setParameters(func_get_args());
$tree = collect([]);
$args['data']->each(function ($item) use(&$tree, $args) {
$currentData = collect([]);
if (intval($item[$this->parent]) == intval($args['parent'])) {
// fill the array with the body fields
foreach ($this->config['body'] as $field) {
$currentData->put($field, isset($item[$field]) ? $item[$field] : null);
}
// Get the child node name
$child = $this->config['childNode'];
$currentData->put($child, []);
$currentData->put($this->parent, $item[$this->parent]);
// Get the primary key name
$item_id = $item[$this->config['primary_key']];
// check the child element
if ($this->hasChild($this->parent, $item_id, $args['data'])) {
// function call again for child elements
$currentData->put($child, $this->renderAsArray($args['data'], $item_id));
}
// current data push to global array
$tree->push($currentData->toArray());
}
});
return $tree->toArray();
}