我需要一些关于以下主题的建议和帮助。
我有这样的迁移设置。
+--------------------+----------------------+--------------------+
| plates | plates_container | container_slots |
+--------------------+----------------------+--------------------+
| id | id | id |
| plate_container_id | number_of_slots(int) | slot |
| container_slot_id | | occupied (boolean) |
| | | plate_container_id |
+--------------------+----------------------+--------------------+
桌子之间的关系是这样的。
印版
- 板
belongsTo
和platecontainer
- 板
belongsTo
containerslot
平板容器
- 平板容器
hasMany
containerSlots
- 平板容器
hasMany
plates
集装箱批次
- 集装箱批次
belongsTo
plateContainer
- 集装箱批次
hasOne
plate
问题
我(由id
指定)如何获取所有number_of_slots
,并检查这些时隙中哪些已经是occupied
,哪些不是。
所以我想澄清一下。像这样或类似的清单就是我想要的。
slots = [
1 => false,
2 => false,
3 => true,
4 => false
//etc
];
如果你对数据库结构有任何其他建议,请在评论中告诉我。我对每次用户为plate
选择slot
之后更新occupied
属性有点怀疑。
Eloquent的收集方法和pluck
将为您提供帮助。假设你已经为提到的每个表正确地定义了你的模型,你可以这样做:
PlateContainer::find($id)
->container_slots
->pluck('occupied', 'id');
这将以以下形式(基本上是一个数组)为您提供您正在寻找的预期结果:
=> Illuminate'Database'Eloquent'Collection {#1859
all: [
1 => true,
2 => true,
3 => false,
4 => true
],
}