Multilevel Notes

//In helper.php

//for Bootstrap Nav
if (! function_exists(‘generateMainTree’)) {
function generateMainTree( $tree, $children, $parentId = 0) {
foreach ($tree as $mainBranch) {
echo ‘<li class=”dropdown”>’;
echo ‘<a href=”#” class=”dropdown-toggle” data-toggle=”dropdown”>’.$mainBranch->name.'<b class=”caret”></b></a>’;
echo ‘<ul class=”dropdown-menu”>’;
generateTree($mainBranch->subCategories, $parentId = $mainBranch->id);
echo ‘</ul>’;
echo ‘</li>’;
}
}
}

if (! function_exists(‘generateTree’)) {
function generateTree( $mainBranch, $parentId = 0) {
foreach($mainBranch as $branch){
if($branch->parent_id == $parentId && $parentId != 0){
if(isset($branch->subCategories) && count($branch->subCategories)>0){
echo ‘<li class=”dropdown dropdown-submenu”>’;
echo ‘<a href=”#” class=”dropdown-toggle” data-toggle=”dropdown”>’.$branch->name.'</a>’;
echo ‘<ul class=”dropdown-menu”>’;
generateTree($branch->subCategories, $branch->id);
echo ‘</ul>’;
echo ‘</li>’;
}
else{
echo ‘<li><a href=”#”>’.$branch->name.'</a></li>’;
}
}

}
return;
}
}

//for select box
if (! function_exists(‘generateFoodCategoryLists’)) {
function generateFoodCategoryLists(array $elements, array $items, $parentId = 0, $indent = 0)
{
foreach ($elements as $key => $element) {
if ($element[‘parent_id’] == $parentId) {
echo ‘<option value =”‘ . $element[‘id’] . ‘” disabled>’;
echo ($indent != 0) ? str_repeat(“&mdash; “, $indent) : ”;
echo $element[‘name’];
foreach ($items as $key => $item) {
if ($item[‘category_id’] == $element[‘id’]) {
echo ‘<option value=”‘ . $item[‘id’] . ‘”>’;
echo ($indent != 0) ? str_repeat(“&nbsp;&nbsp; “, $indent) : ”;
echo $item[‘name’];
echo ‘</option>’;
}
}
$children = generateFoodCategoryLists($elements, $items, $element[‘id’], $indent + 1);
}
}
}
}
—————————————————————————————————–
To use Helper, add “”files”: [“app/Http/Helpers/helpers.php”]” in autoload section.

Eg.

“autoload”: {
“classmap”: [
“database”
],
“psr-4”: {
“App\\”: “app/”
},
“files”: [“app/Http/Helpers/helpers.php”]
}
—————————————————————————————————–
In blade,
{!! generateMainTree($parents, $children, $parentId=0) !!}
—————————————————————————————————–
In Controller,

In Repo
public function getParentByMenuType($type){
$objs = Menudetail::leftjoin(‘menu’, ‘menu.id’, ‘=’, ‘menu_detail.menu_id’)
->select(‘menu_detail.*’)
->where(‘menu.category’,’=’,$type)
->where(‘menu_detail.parent_id’, ‘=’, 0)
->get();
return $objs;
}
$result = $menuDetailRepo->getParentByMenuType(2); //2 is for main menu

foreach($result as $k=>$v){
$subresult = $this->categoriesTree($result[$k]);
$result[$k]->subCategories=$subresult;
}

function categoriesTree($result){
$id=$result->id;
$menuDetailRepo = new MenuDetailRepository();
$sresult = $menuDetailRepo->getChildrenByID($id);
foreach($sresult as $k=>$v){
$subresult = $this->categoriesTree($sresult[$k]);
$sresult[$k]->subCategories= $subresult;
}
return $sresult;
}

public function getChildrenByID($id)
{
$objs = DB::table(‘menu_detail’)->where(‘parent_id’,’=’,$id)->get();
return $objs;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s