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
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