class Category extends Model
{
// we consider item table has `category_id` field to maintain relationship
public $hasMany = [
'items' => ['Yournamespace\Item']
];
}
现在你所能得到的就是获取类别
$categories= Category::all();
// pass $categories to view
现在循环浏览类别及其项目
<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 作为 main list 和 each list have sub-list 作为 it's items
1 回答
可以是你可以利用亲子关系的船 .
它将
show list of categories
作为 main list 和each list have sub-list
作为 it's items如果有任何疑问请评论 .