我在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”)。我想给所有的父母看,看起来像这样:"测试-〉测试的子类别-〉测试的子类别"我该怎么做?
6条答案
按热度按时间pu82cl6c1#
您可以在
Cateory
模型上定义一个模型访问器,用于检索所有父对象并将它们放入一个集合中。因此,在Category
模型中添加以下方法:然后在你的刀片模板中你可以获取一个类别的所有父类
我认为没有其他解决方案不同于递归地检索所有父类和@Ross Wilson在评论中建议的包。考虑到查询的数量将等于父类别的数量。
您可以做的另一件事是在
categories
表中将父树设置到DB中,以保存大量查询。在categories
表中添加一个类似parents_tree
的字符串字段,并在保存/更新时使用已连接的父名称设置此值。这是一个有点脏,因为如果其中一个父母改变了它的名字,你必须相应地更新所有的孩子。取决于你想要屈服于哪一种权衡。dced5bon2#
你可以添加父变量:
这是最短的解决方案,但这不是最好的方法,因为每次加载父对象时都是如此。最好这样做:
class Category extends Model { protected $table = 'categories';
在你的剑中你呼唤:
{{ $category->getParentsNames() }}
ehxuflar3#
我知道这是旧的,但这里是正确的答案,任何人在未来。这将让你无限的类别
在类别模型中
在Blade中(我相信你也可以在模型级别上做到这一点,但这里是我的快速方法)
结果首页/客厅/沙发/沙发
fslejnso4#
到目前为止,我发现的最佳解决方案是使用嵌套集模型。
详细的解释/实施是很长的,已经有很多主题了。所以我会缩短主题。
其思想是将具有介于父值之间的值的类别分组,以此类推。例如:
使用这些值,仅使用一个查询就可以很容易地查询所有的上行类别或下行类别,因为它们总是包含在两个值之间。
一个主要的缺点是;当类别发生变化(创建/删除/移动)时,需要重新评估和更新节点。
以下是我找到的最好的解释:http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
我还发现这个库似乎工作得很好https://github.com/lazychaser/laravel-nestedset
hmae6n7t5#
我的唯一注意到得到的主要类别,也许这将是有用的人。
blade.php
2guxujil6#
//类别模型:
//控制器: