Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Providers;
- use App\Category;
- use App\Services\Tree;
- use Illuminate\Support\ServiceProvider;
- use Illuminate\Support\Facades\View;
- class AppServiceProvider extends ServiceProvider
- {
- /**
- * Bootstrap any application services.
- *
- * @return void
- */
- public function boot()
- {
- $this->menuLoad();
- }
- /**
- * Register any application services.
- *
- * @return void
- */
- public function register()
- {
- }
- private function generateMenu(&$elements, $parentId = 0){
- $branch = array();
- foreach ($elements as $element) {
- if ($element['parent_id'] == $parentId) {
- $children = $this->generateMenu($elements, $element['id']);
- if ($children) {
- $element['children'] = $children;
- }
- $branch[$element['id']] = $element;
- unset($elements[$element['id']]);
- }
- }
- return $branch;
- }
- public function menuLoad()
- {
- $categories = Category::with(['descendants', 'products'])->get()->toArray();
- $tree = new Tree($categories);
- $menu = $tree->build(0);
- View::composer('layout.base', function($view) use($menu){
- $view->with(['categories' => collect($menu), 'delimiter' => '', 'nesting' => 0]);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement