[英]How to reverse unlimited sub categories in Laravel using recursive relations
我使用遞歸關系開發了一個無限的子類別系統,以獲得盡可能多的子類別級別。 例如,如果我在一個類別為 1 的頁面中,我將看到子類別類別 11 和類別 12。如果我 go 到類別 11 的頁面,我將根據最后一個類別看到其他子類別11,例如111類、112類和113類等。
為此,我在我的類別 model 中創建了 2 個關系:
public function categories()
{
return $this->hasMany(Categories::class);
}
public function childrenCategories()
{
return $this->hasMany(Categories::class)->with('categories');
}
在我的類別 Controller 之后,我向我的數據庫發出了一個請求:
$categoriesTree = Categories::whereNull('categories_id')
->with('childrenCategories')
->get();
(...)
return view('categories.index', compact('categoriesTree'));
在我的刀片視圖中,我做了:
<select class="form-control select2">
<option value="0">Choose a category</option>
@php $nb = 0; @endphp
@foreach($categoriesTree as $category)
<option value="{{ $category->id }}" data-name="{{ $category->name_fr }}">{{ $category->name_fr }}</option>
@foreach ($category->childrenCategories as $childCategory)
@include('categories.child_categories', ['child_categories' => $childCategory, 'nb' => $nb])
@endforeach
@endforeach
</select>
包含的 child_categories 文件是:
@php $nb++; @endphp
<option value="{{ $child_categories->id }}" data-name="{{ $child_categories->name_fr }}">{{ ' ' . str_repeat('---', $nb) . ' ' . $child_categories->name_fr }}</option>
@foreach ($child_categories->childrenCategories as $childCategory)
@include('categories.child_categories', ['child_categories' => $childCategory])
@endforeach
我的問題是:如何獲得反向無限子類別來獲得像這張圖片一樣的面包屑? 例如,如果我屬於 111 類,我希望看到這個:
我不知道該怎么做。 謝謝!!
你可以試試這樣:
// Category.php
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id');
}
// To access CategoryController.php
$category->children; // sub-categories collection
$category->parent; // parent instance
你可以像這樣得到它的父母
public function parent()
{
return $this->belongsTo(Categories::class)->with('parent');
}
Category::with('parent')->first();
我找到了如何做一個遞歸方法,感謝您對 belongsTo() 方法的建議,我可以做一個遞歸刀片:
// Categories model
public function parentsCategories()
{
return $this->belongsTo(Categories::class, 'categories_id')->with('parentsCategories');
}
// Categories Controller
$categoriesBreadcrumb = Categories::where('id', $category_id)
->with('parentsCategories')
->first();
//index.blade.php
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
@if(is_object($categoriesBreadcrumb))
@include('categories.parents_categories', ['categoriesBreadcrumb' => $categoriesBreadcrumb->parentsCategories])
<li class="breadcrumb-item">
{{ $categoriesBreadcrumb->name_fr }}
</li>
@endif
</ol>
</nav>
// parents_categories.blade.php (recursive)
@if(is_object($categoriesBreadcrumb))
@include('categories.parents_categories', ['categoriesBreadcrumb' => $categoriesBreadcrumb->parentsCategories])
<li class="breadcrumb-item">
<a href="{{ route('collections.index', ['category_id' => $categoriesBreadcrumb->id ]) }}">{{ $categoriesBreadcrumb->name_fr }}</a>
</li>
@endif
所以最終的面包屑結果是:如果我在類別 1111 中,我可以看到:
1 類 / 11 類 / 111 類 / 1111 類
太感謝了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.