Skip to content

Instantly share code, notes, and snippets.

@artemrogov
Created October 29, 2019 09:54
Show Gist options
  • Save artemrogov/746e95f283d9ce9521b494d5ca910c14 to your computer and use it in GitHub Desktop.
Save artemrogov/746e95f283d9ce9521b494d5ca910c14 to your computer and use it in GitHub Desktop.
рекурсивное удаление элементов меню
/**
* рекурсивное удаление элементов меню
*/
public function destroyRecursive($id){
$parent = Menu::find($id);
$arrayOfIds = $this->getChildrenMenuDel($parent);
array_push($arrayOfIds,$id);
Menu::destroy($arrayOfIds); // этот стандарный метод моделей в ORM Eloquent который выполняет операцию удаления
}
private function getChildrenMenuDel($menu){
$ids = [];
foreach ($menu->children as $item) { // пулучить детей
$ids[] = $item->id; //положить их ids в массив
$ids = array_merge($ids, $this->getChildrenMenuDel($item)); // объединить их в массив
}
return $ids; // вернуть объединенные id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment