Advertisement
Guest User

Untitled

a guest
May 12th, 2020
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.42 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Providers;
  4.  
  5. use App\Category;
  6. use App\Services\Tree;
  7. use Illuminate\Support\ServiceProvider;
  8. use Illuminate\Support\Facades\View;
  9.  
  10.  
  11. class AppServiceProvider extends ServiceProvider
  12. {
  13.  
  14.  
  15.     /**
  16.      * Bootstrap any application services.
  17.      *
  18.      * @return void
  19.      */
  20.     public function boot()
  21.     {
  22.         $this->menuLoad();
  23.     }
  24.  
  25.     /**
  26.      * Register any application services.
  27.      *
  28.      * @return void
  29.      */
  30.     public function register()
  31.     {
  32.  
  33.     }
  34.  
  35.     private function generateMenu(&$elements, $parentId = 0){
  36.         $branch = array();
  37.  
  38.         foreach ($elements as $element) {
  39.             if ($element['parent_id'] == $parentId) {
  40.                 $children = $this->generateMenu($elements, $element['id']);
  41.                 if ($children) {
  42.                     $element['children'] = $children;
  43.                 }
  44.                 $branch[$element['id']] = $element;
  45.                 unset($elements[$element['id']]);
  46.             }
  47.         }
  48.         return $branch;
  49.     }
  50.  
  51.     public function menuLoad()
  52.     {
  53.  
  54.         $categories = Category::with(['descendants', 'products'])->get()->toArray();
  55.         $tree = new Tree($categories);
  56.         $menu = $tree->build(0);
  57.  
  58.  
  59.  
  60.         View::composer('layout.base', function($view) use($menu){
  61.             $view->with(['categories' => collect($menu), 'delimiter' => '', 'nesting' => 0]);
  62.         });
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement