简体   繁体   English

如何使用递归关系反转 Laravel 中的无限子类别

[英]How to reverse unlimited sub categories in Laravel using recursive relations

I developped an unlimited sub categories system using recursive relations to get as many sub categories levels as it could find.我使用递归关系开发了一个无限的子类别系统,以获得尽可能多的子类别级别。 For example, if I'm in one page with Category 1, I will see the sub categories Category 11 and Category 12. And if I go to to the page with Category 11, I will see the others sub categories depending on this last Category 11, for example Category 111, Category 112 and Category 113. Etc.例如,如果我在一个类别为 1 的页面中,我将看到子类别类别 11 和类别 12。如果我 go 到类别 11 的页面,我将根据最后一个类别看到其他子类别11,例如111类、112类和113类等。

This image is what I get with the following code:这张图片是我通过以下代码得到的: 在此处输入图像描述

For this, I created 2 relations in my Categories model:为此,我在我的类别 model 中创建了 2 个关系:

public function categories()
{
    return $this->hasMany(Categories::class);
}

public function childrenCategories()
{
    return $this->hasMany(Categories::class)->with('categories');
}

After in my Categories Controller I made a request to my database with relations:在我的类别 Controller 之后,我向我的数据库发出了一个请求:

$categoriesTree = Categories::whereNull('categories_id')
     ->with('childrenCategories')
     ->get();

(...)

return view('categories.index', compact('categoriesTree'));

And in my blade view, I did:在我的刀片视图中,我做了:

<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>

And the child_categories file included is:包含的 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

My question is : How can I get a reverse unlimited sub categories to have breadcrumb like this image?我的问题是:如何获得反向无限子类别来获得像这张图片一样的面包屑? For example, if I'm in category 111, I would like to see this:例如,如果我属于 111 类,我希望看到这个: 在此处输入图像描述

I have no idea how to do this.我不知道该怎么做。 Thanks!!谢谢!!

You Can Try This Way:你可以试试这样:

// 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

You can get its parents like this你可以像这样得到它的父母

public function parent()
{
    return $this->belongsTo(Categories::class)->with('parent');
}
Category::with('parent')->first();

I found how to do a recursive method, thanks because of your advices about the belongsTo() method, I could do a recursive blade:我找到了如何做一个递归方法,感谢您对 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

So the final breadcrumb result is: if I'm in Category 1111, I can see:所以最终的面包屑结果是:如果我在类别 1111 中,我可以看到:

Category 1 / Category 11 / Category 111 / Category 1111 1 类 / 11 类 / 111 类 / 1111 类

Thank you so much!太感谢了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM