Laravel获取类别的所有父类

kadbb459  于 2023-05-01  发布在  其他
关注(0)|答案(6)|浏览(165)

我在laravel中有类别和子类别

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table = 'categories';

    public function parent()
    {
        return $this->belongsTo('App\Category', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('App\Category', 'parent_id');
    }
}

我可以添加一个新的类别,它属于一个已经创建的类别。类似于“Test-〉subcategory of test”,然后假设我将创建“sub-subcategory of test”,它属于子类别的测试,看起来像这样:"Test-〉subcategory of test-〉sub-subcategory of test"在视图中,我想打印类别的父类别(如果有的话)。我是这样做的:

@if($category->parent)
   <td>{{ $category->parent->name }} <strong>-></strong> {{ $category->name }}</td>
@else
   <td>{{ $category->name }}</td>    
@endif

但这只显示了一个父类别(“subcategory of test-〉sub-subcategory of test”)。我想给所有的父母看,看起来像这样:"测试-〉测试的子类别-〉测试的子类别"我该怎么做?

pu82cl6c

pu82cl6c1#

您可以在Cateory模型上定义一个模型访问器,用于检索所有父对象并将它们放入一个集合中。因此,在Category模型中添加以下方法:

public function getParentsAttribute()
{
    $parents = collect([]);

    $parent = $this->parent;

    while(!is_null($parent)) {
        $parents->push($parent);
        $parent = $parent->parent;
    }

    return $parents;
}

然后在你的刀片模板中你可以获取一个类别的所有父类

@if(count($category->parents))
    <td>{{ $category->parents->implode('-') }} <strong>-></strong> {{ $category->name }}</td>
@else
    <td>{{ $category->name }}</td>
@endif

我认为没有其他解决方案不同于递归地检索所有父类和@Ross Wilson在评论中建议的包。考虑到查询的数量将等于父类别的数量。
您可以做的另一件事是在categories表中将父树设置到DB中,以保存大量查询。在categories表中添加一个类似parents_tree的字符串字段,并在保存/更新时使用已连接的父名称设置此值。这是一个有点脏,因为如果其中一个父母改变了它的名字,你必须相应地更新所有的孩子。取决于你想要屈服于哪一种权衡。

dced5bon

dced5bon2#

你可以添加父变量:

class Category extends Model
{
    protected $table = 'categories';

    protected $appends = [
        'parent'
    ];

    public function parent()
    {
        return $this->belongsTo('App\Category', 'parent_id');
    }
}

这是最短的解决方案,但这不是最好的方法,因为每次加载父对象时都是如此。最好这样做:
class Category extends Model { protected $table = 'categories';

public function parent()
{
    return $this->belongsTo('App\Category', 'parent_id');
}

public function getParentsNames() {
    if($this->parent) {
        return $this->parent->getParentsNames(). " > " . $this->name;
    } else {
        return $this->name;
    }
}

在你的剑中你呼唤:{{ $category->getParentsNames() }}

ehxuflar

ehxuflar3#

我知道这是旧的,但这里是正确的答案,任何人在未来。这将让你无限的类别
在类别模型中

public function parent()
{
    return $this->belongsTo('App\Category', 'parent_id');
}

public function getParentsNames() {

    $parents = collect([]);

    if($this->parent) { 
        $parent = $this->parent;
        while(!is_null($parent)) {
            $parents->push($parent);
            $parent = $parent->parent;
        }
        return $parents;
    } else {
        return $this->name;
    }

}

在Blade中(我相信你也可以在模型级别上做到这一点,但这里是我的快速方法)

@if ($category->getParentsNames() !== $category->name)
                                @foreach ($category->getParentsNames()->reverse() as $item)
                                    @if ($item->parent_id == 0)
                                        <li class="breadcrumb-item"><a href="/{{ $item->slug }}">{{ $item->name }}</a></li>
                                             @else
                                        <li class="breadcrumb-item"><a href="../{{ $item->slug }}">{{ $item->name }}</a></li>
                                    @endif
                                @endforeach  
                            @endif

结果首页/客厅/沙发/沙发

fslejnso

fslejnso4#

到目前为止,我发现的最佳解决方案是使用嵌套集模型
详细的解释/实施是很长的,已经有很多主题了。所以我会缩短主题。
其思想是将具有介于父值之间的值的类别分组,以此类推。例如:

Clothes : 1-10
    - T-shirts: 2-3
    - Pants: 4-9
        - Shorts 5-6
        - Joggers 7-8

使用这些值,仅使用一个查询就可以很容易地查询所有的上行类别或下行类别,因为它们总是包含在两个值之间。
一个主要的缺点是;当类别发生变化(创建/删除/移动)时,需要重新评估和更新节点。
以下是我找到的最好的解释:http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
我还发现这个库似乎工作得很好https://github.com/lazychaser/laravel-nestedset

hmae6n7t

hmae6n7t5#

我的唯一注意到得到的主要类别,也许这将是有用的人。

static function  getDepth($parent_id) {

       $categories= Categories::find($parent_id);

       if ($categories) {
           if ($categories->parent_id == 0) {
               return $categories->category_name;
           } else {
               return self::getDepth($categories->parent_id);
           }
       }

    }

blade.php

<td>{{ \App\Models\Admin\Category::getDepth($category->parent_id)}}</td>
2guxujil

2guxujil6#

//类别模型:

public function subCategories()
{
    return $this->hasMany(Category::class, parent_id')->with('subCategories');
}

public function parentCategory()
{
    return $this->belongsTo(Category::class, 'parent_id');
}

//控制器:

public function getAllCategoryWithChild(Request $request) {
    $categories = Category::with(['subCategories','parentCategory.subCategories']);
    $categories->whereDoesntHave('parentCategory');
    $categories = $categories->paginate($request->get('per_page', 10));
    return CategoryResource::collection($categories)
}

相关问题