【问题标题】:Builder plugin how to display items selected by another model class instead of all items?Builder插件如何显示由另一个模型类而不是所有项目选择的项目?
【发布时间】:2018-04-07 14:48:35
【问题描述】:
我使用 octoberCMS Builder 插件构建了自己的插件。
有 2 个不同的模型类
- 分类
- 项目
在模型类“项目”中,我与模型类类别有关系,因此每个项目都可以链接到一个“类别”。
在我的网页上,我想显示模型类类别以及模型类“项目”中链接到该类别的所有项目。
但是,现在显示所有项目,而不是链接到类别的项目。
我的想法是使用 == 符号,但到目前为止还没有奏效。
我该如何解决这个问题?非常感谢您的帮助!
explanation of my question
what my page looks like
【问题讨论】:
标签:
octobercms
octobercms-plugins
octobercms-builder
【解决方案1】:
也许你可以利用父子关系。
在您的Category 模型中添加关系
class Category extends Model
{
// we consider item table has `category_id` field to maintain relationship
public $hasMany = [
'items' => ['Yournamespace\Item']
];
}
现在你只能获取Categories
$categories= Category::all();
// pass $categories to view
现在循环遍历category 和its items
<ul>
{% for category in categories%}
<li>
<h3> {{ category.name }} </h3>
<ul>
{% for item in category.items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
它将show list of categories作为主列表和each list have sub-list作为它的项目
如有任何疑问,请发表评论。