How to create a database-driven multi-level navigation menu using Laravel

前端 未结 3 949
情歌与酒
情歌与酒 2020-12-02 15:06

I\'m new to Laravel 4 and I\'m totally confused about it\'s models. I\'m trying to create a database-driven navigation menu for my project and all I know is I have to create

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 15:20

    So after doing much more searching and reading from different sources this is what I came up with and it's working fine:

    /app/models/Navigation.php

    hasOne('navigation', 'id', 'parent_id');
    
        }
    
        public function children() {
    
            return $this->hasMany('navigation', 'parent_id', 'id');
    
        }  
    
        public static function tree() {
    
            return static::with(implode('.', array_fill(0, 4, 'children')))->where('parent_id', '=', NULL)->get();
    
        }
    
    }
    

    /app/controllers/HomeController.php

    layout->content = View::make('layouts.home.index')->withItems($items);
    
        }
    
    }
    

    /app/views/layouts/home/index.blade.php

      @foreach($items as $item)
    • {{ $item->title }} @foreach($item['children'] as $child)
    • {{ $child->title }}
    • @endforeach @endforeach

提交回复
热议问题