laravel - 从相关表中获取数据到 laravel 中的 vuejs 组件
问题描述
我的 vue 组件在每个视图中作为侧边栏加载,使用 3 个级别的对象,我将根据数据库数据动态填充它
data() {
return {
collection: [
{
id: 1,
title: "Blue Collection",
categories: [
{
id: 1,
title: "Large Format",
items: [
{
id: 1,
title: "BL 250",
},
{
id: 2,
title: "BL 260",
},
]
},
{
id: 2,
title: "Small Format",
items: []
}
]
},
{
id: 2,
title: "Lavender Collection",
categories: [
{
id: 1,
title: "Medium Format",
items: []
},
{
id: 2,
title: "Small Format",
items: []
},
]
},
{
id: 3,
title: "Violet Collection",
categories: [
{
id: 1,
title: "Large Format",
items: [
{
id: 1,
title: "VL 400",
},
{
id: 2,
title: "VL 420",
},
{
id: 3,
title: "VL 300",
},
]
},
]
},
],
}
},
我怎样才能collection: []
在开始时有空,根据当前数据库表动态填充
我当前的表格模型
// Grand Parent
class Collection extends Model
{
public function categories() {
return $this->hasMany('App\Category', 'collection_id');
}
public function items() {
return $this->hasManyThrough('App\Item', 'App\Category');
}
}
// Parent
class Category extends Model
{
public function items() {
return $this->hasMany('App\Item', 'category_id');
}
public function collection() {
return $this->belongsTo('App\Collection');
}
}
// Child
class Item extends Model
{
public function categories() {
return $this->belongsTo('App\Item', 'item_id');
}
}
而且我不知道正确Controller
,因为它是侧边栏视图的一部分
class CollectionController extends Controller
{
public function index() {
$collection = Collection::with('categories')->get();
// according to sidebar position which uses in all pages, which view needed or is it correct way to load data here?
return view('???')->with(['collection' => $collection]);
}
}
解决方案
推荐阅读
- c# - C#如何在值为零时禁用按钮
- c++ - 如何将 com 实例转换为变体,在 idispach 调用中传递它
- ruby - 检查字符串数组中的值
- python - 时间复杂度:嵌套 for 循环中的增长列表
- dockerfile - 在 docker 中安装 Redmine 插件时出现“无法找到 Gemfile”
- java - Java 中的字符数不会累加
- reactjs - 跳过效果不适用于动态 URL 数组
- python-3.x - 熊猫:我可以按一列的条目分组并根据另一列的条目对其元素求和吗
- c - 如何找出用户输入在 C 中包含非 ASCII 字符
- python - Python 中的自定义信号