123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- namespace App\Services;
- use App\Models\SiteMenu;
- class MenuService
- {
-
- public function getMultiLevelMenu(int $menu_location=0,int $dist_id = 0): array
- {
-
- $menus = SiteMenu::where('show', 1)
- ->where('dist_id', $dist_id)
- ->where('menu_location', $menu_location)
- ->orderBy('order', 'asc')
- ->get();
-
- return $this->buildMenuTree($menus);
- }
-
- private function buildMenuTree($menus, $parentId = 0): array
- {
- $menuTree = [];
- foreach ($menus as $menu) {
- if ($menu->parent_id == $parentId) {
- $menu->children = $this->buildMenuTree($menus, $menu->id);
- $menuTree[] = $menu->toArray();
- }
- }
- return $menuTree;
- }
- }
|