Laravel获得所有类别的父母 [英] Laravel get all parents of category

查看:101
本文介绍了Laravel获得所有类别的父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在laravel中有类别和子类别

I have categories and subcategories in 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');
    }
}

然后我可以添加一个新类别,该类别属于已经创建的类别.类似于测试->测试子类别",然后假设我将创建属于测试子类别的测试子类别",如下所示:测试->测试子类别->测试子类别" 在视图中,我想打印类别的父项(如果有). 我是这样的:

And I can add a new category which belongs to an already created category. Something like "Test -> subcategory of test" and then let's say I will create "sub-subcategory of test" which belongs to subcategory of test which will look like this: "Test -> subcategory of test -> sub-subcategory of test" And in the view I want to print the parents of the category if it has any. I do it like this:

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

但这仅显示一个父类别(测试的子类别->测试的子子类别").我想显示所有如下所示的父母:"Test-> test的子类别-> test的子子类别" 我该怎么办?

But this only shows one parent category ("subcategory of test -> sub-subcategori of test"). I want to show all the parents which will look like this: "Test->subcategory of test -> sub-subcategory of test" How can I do that?

推荐答案

您可以在Cateory模型上定义一个模型访问器,该模型访问器将检索所有父级并将其放入集合中.因此,在您的Category模型中,添加以下方法:

You could define a model accessor on Cateory model that retrieves all parents and puts them into a collection. So in your Category model add the following method:

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

    $parent = $this->parent;

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

    return $parents;
}

然后在刀片模板中,您可以获取类别的所有父项

then in your blade template you can fetch all the parents of a category

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

除了递归检索所有父母和@Ross Wilson在评论中建议的软件包外,我没有其他解决方案.考虑到查询数量将等于父类别的数量.

I see no other solution different from recursively retrieving all the parents and the package suggested by @Ross Wilson in the comments. Take into account that the number of queries will be equal to the number of the parent categories.

您可以做的另一件事是将父树设置到categories表中的数据库中,以节省大量查询.在categories表中添加一个类似于parents_tree的字符串字段,并在保存/更新时将此值与已经连接的父级名称一起设置.这有点脏,因为如果其中一位父母更改了名字,则必须相应地更新所有孩子.由您决定要舍弃哪些权衡因素.

Another thing that you could do is setting the parent tree into the DB in the categories table, to save a lot of queries. Add in your categories table a string field like parents_tree and at save/update time set this value with the parent names already concatenated. This is a little dirty because if one of the parents change its name you have to update all the children accordingly. Is up to you to which trade-off you want to succumb.

这篇关于Laravel获得所有类别的父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆