This is result I get from database (mysql). Now I would like to get menu with structure like
department title1
- project1
- project2
department title2
- project3
- project4
....
This are my results
project title | department title | department_id
project1 | department1 | 9
project2 | department2 | 2
project3 | department3 | 1
project11 | department1 | 9
project23 | department2 | 2
project24 | department3 | 1
project15 | department1 | 9
project26 | department2 | 2
project21 | department4 | 4
I'm using laravel 4.2.(and this will be in blade syntax) if this is any help.
Here's what I tried so far: In my controller
//projects is above results from db query builder
foreach($projects as $project)
{
if($project->department_id != "")
{
$department = array(
"id" => $project->department_id,
"title" => $project->department_title,
);
$departments_projects[] = array_push($departments_projects,$department);
}
}
And then in my view (where I'm build the menu) I have this code
<ul class="sub-menu">
@foreach($departments as $department)
<li class="">
<a href="javascript:;">
{{ $department['title'] }} <i class="icon-arrow"></i>
</a>
<ul class="sub-menu">
@foreach($projects as $project)
@if($project->department_id == $department['id'] && $project->title != "")
<li>
<a href="{{ route('department.project.show',array($project->department_id,$project->id)) }}">
{{ $project->title }}
</a>
</li>
@endif
@endforeach
</ul>
</li>
@endforeach
</ul>