#23 Make E-com website in Laravel 5.6 | #34 Admin Panel | Display Categories at Left Sidebar

preview_player
Показать описание

In Part 23, we are going to display Categories and Sub Categories dynamically at left sidebar of home page.

There are 2 ways of doing this task :-

1) Basic Approach (Without Relations)

2) Advance Approach (With Relations, Recommended)

1) Basic Approach (Without Relations) :-

1.1) Update index function :-

1.2) Update Headers :-

use App\Category;

1.3) Update index function :-

Now see we are getting all categories and sub categories. And now we will create one variable in which we will add all these categories and sub categories with html that we are using to display at home page.

Now remove all html part and simply add $categories_menu variable that we have generated in index function.

Please correct all JS paths before proceed further.

Just open front_design blade file and make sure your JS paths looks like below for all js:

And after that it will work fine..

Now see, all categories and sub categories are coming at left side of home page correctly.

2) Advance Approach (With Relations, Recommended)

2.1) Update Category Model

2.2) Update index function

Update categories query with this relation categories by adding with('categories') in query.

Now remove $categories_menu that we have added earlier and add foreach loops in html only.

See now in video, our categories and sub categories are coming fine. And this approach with relation is more user friendly and faster/easier to do.

In next video, we will start working on category pages in which only products of that category will come..

So, stay tune for next video...

Thanks for watching :)

Рекомендации по теме
Комментарии
Автор

I have a question: how do you move a selected block of code like at 24:07 in left or right? I am using VS Code, but at least I want to know how to do it in Sublime.

artellas
Автор

main categories id href='#".$cat->id."' and sub categories div id will be same <div id='".$cat->id."'

aminurrashid
Автор

Relations are usually created between two tables but here you have created relation between different columns of a single table. My question is, you have previously taught how to fetch category names using parent id but when you are using a relation then for sure there must be an SQL Statement running behind the scenes. Could you plz explain that SQL Statement?

Zaafran
Автор

@foreach($categories as $cat)
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordian" href="#{{$cat->id}}">
<span class="badge pull-right"><i class="fa fa-plus"></i></span>
{{ $cat->name }}
</a>
</h4>
</div>
<!--for the drop down, sub categoris -->
<div id="{{ $cat->id }}" class="panel-collapse collapse">
<div class="panel-body">
<ul>
@foreach($cat->categories as $subcat)
<li><a href="#"> {{$subcat->name}}</a></li>
@endforeach


please explain to me how are we gettting teh value of the subcategories please, and what is this (categories) you call on @foreach($cat->categories as $subcat)

janpaulalmanoche
Автор

hi bro, thank you for the amazing job here on your youtube channel .i have one problem, the subcategory unable to pop-down
I don't know what I am doing wrong:This is the code:

$categories=

$catmenu="";

foreach($categories as $cat){

$catmenu.=" <div class='panel-heading'>
<h4 class='panel-title'>
<a data-toggle='collapse' data-parent='#accordian' href=".$cat->id.">
<span class='badge pull-right'><i class='fa fa-plus'></i></span>

".$cat->name."
</a>
</h4>
</div>
<div id=".$cat->id." class='panel-collapse collapse'>
<div class='panel-body'>
<ul>";

$subCategories=

foreach($subCategories as $sub_cat){

$catmenu.=" <li><a href=".$sub_cat->id."> ".$sub_cat->name."</a></li>";
}

$catmenu.="
</ul>
</div>
</div>

";

}

return \View::make('index')->with(compact('productsAll', 'catmenu', 'cat', 'sub_cat'));

//return \view('index', \compact('productsAll', 'categories_Menu'));

//return \view('index', \compact('productsAll', 'categories_Menu'));
}
this is my blade template where echo the 'catmenu' variable.Pls, i need your help, help me.Thanks in adavnce.

afolabibusari
Автор

is there is way that I can download indexconroller.php files on this section! after all i have followed your steps and i have to many errors every time i fix, the new error showed up. but i total understand about this code just want move to next chapter. so it is possible for me to donwload this indexcontroller.php!

Ali-njtv
Автор

Hi sir,
$categories = 0])->get();
i wanted all Category show
fix
Thank you

manarhnezel
Автор

what is the shortkey to unfold a div? you always use but i can't find it

waqarmughal
Автор

why you don't correct the paths for fonts?

pesca_man
Автор

sir how to add one product in two different category??and show them in like latest-products/trending products.

mrjeetmumbaikar
Автор

Hello, thanks for all things. I am having an issue that i can not make category-tab run correctly so i hope you can code this part and show me, plsss. Sorry for my English. i am not good at English

tuannguyenduc
Автор

i can't desplay sub_categories, browser give the errore of invalid argument supplied for foreach().

Adeel
Автор

please send me the complete source code of this e commerce application

Sport-word
Автор

hi bro, thank you for the amazing job here on your youtube channel .i have one problem, the subcategory unable to pop-down
I don't know what I am doing wrong:This is the code:

$categories=

$catmenu="";

foreach($categories as $cat){

$catmenu.=" <div class='panel-heading'>
<h4 class='panel-title'>
<a data-toggle='collapse' data-parent='#accordian' href=".$cat->id.">
<span class='badge pull-right'><i class='fa fa-plus'></i></span>

".$cat->name."
</a>
</h4>
</div>
<div id=".$cat->id." class='panel-collapse collapse'>
<div class='panel-body'>
<ul>";

$subCategories=

foreach($subCategories as $sub_cat){

$catmenu.=" <li><a href=".$sub_cat->id."> ".$sub_cat->name."</a></li>";
}

$catmenu.="
</ul>
</div>
</div>

";

}

return \View::make('index')->with(compact('productsAll', 'catmenu', 'cat', 'sub_cat'));

//return \view('index', \compact('productsAll', 'categories_Menu'));

//return \view('index', \compact('productsAll', 'categories_Menu'));
}
this is my blade template where echo the 'catmenu' variable

<div class="panel-group category-products" id="accordian" ><!--category-productsr-->
<div class="panel panel-default">
<?php

if(!empty($catmenu)){

echo $catmenu; }
?>

</div> <!--/category-products-->
</div>




.Pls, i need your help, help me.Thanks in adavnce.

afolabibusari
welcome to shbcf.ru