Skip to content

Instantly share code, notes, and snippets.

@stevebauman
Last active August 29, 2015 14:21
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 stevebauman/00aaeaa0c7ca260a0aa5 to your computer and use it in GitHub Desktop.
Save stevebauman/00aaeaa0c7ca260a0aa5 to your computer and use it in GitHub Desktop.
Recursive BOM Array Method
/**
* Returns all of the assemblies items in an
* easy to work with array.
*
* @param bool $recursive
*
* @return array
*/
public function getAssemblyItemsList($recursive = true, $depth = 0)
{
$list = [];
$items = $this->getAssemblyItems();
$level = 0;
$depth++;
foreach ($items as $key => $item) {
$list[$level] = [
'id' => $item->id,
'name' => $item->name,
'metric_id' => $item->metric_id,
'category_id' => $item->category_id,
'quantity' => $item->pivot->quantity,
'depth' => $depth,
];
if ($item->is_assembly && $recursive) {
$list[$level]['parts'] = $item->getAssemblyItemsList(true, $depth);
}
$level++;
}
return $list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment