I have created nested treeview categories and subcategories but seems that it is rendering the categories twice.
This is what is happen and I see on page.
Main category 1
- Sub -category 1.1
- Sub -category 1.2
Main category 2
- Sub -category 2.1
- Sub -category 2.2
Sub -category 1.1
Sub -category 1.2
Sub -category 2.1
Sub -category 2.2
What is happening here is that first is render the correct treeview but then under is render sub-categories as main categories.
The categories and sub-categories are saved in one table where I have column parent_id 0 for main category and if is sub-category is the id of main-category instead. This is my model
public function parent()
{
return $this->belongsTo('App\Category');
}
public function children()
{
return $this->hasMany('App\Category', 'parent_id');
}
Controller
public function index()
{
$allCategories = Category::with(['children'])->get();
return view('categories', compact('allCategories'));
}
and the blade
@foreach($allCategories as $category)
<li class="treeview">
<a href=""></a>
<ul class="treeview-menu">
@foreach($category->children as $subcategory)
<li class=""><a href=""></a></li>
@endforeach
</ul>
</li>
@endforeach
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire