Created
October 29, 2019 09:54
-
-
Save artemrogov/746e95f283d9ce9521b494d5ca910c14 to your computer and use it in GitHub Desktop.
рекурсивное удаление элементов меню
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* рекурсивное удаление элементов меню | |
*/ | |
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