Laravel:获取所有值并根据布尔属性进行检查

时间:2015-12-02 13:20:35

标签: php mysql laravel eloquent

我需要一些有关以下主题的建议和帮助。

我有一个像这样的迁移设置。

+--------------------+----------------------+--------------------+
|       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

PlateContainer

  • platecontainer hasMany containerSlots
  • platecontainer hasMany plates

ContainerSlot

  • ContainerSlot belongsTo plateContainer
  • ContainerSlot hasOne plate

问题

我如何(由id指定)获取所有number_of_slots并检查哪些广告位已经occupied哪些广告位已经slots = [ 1 => false, 2 => false, 3 => true, 4 => false //etc ];

所以要澄清一点。像这样或类似的列表是我要求的。

occupied

如果您对数据库结构有任何其他建议,请在评论中告诉我。在用户为slot选择plate后,每次更新 my_analyzer: type: custom tokenizer: keyword filter: lowercase products_filter: type: "nested" properties: filter_name: {"type" : "string", analyzer: "my_analyzer"} filter_value: {"type" : "string" , analyzer: "my_analyzer"} 属性时,我都有点怀疑。

1 个答案:

答案 0 :(得分:1)

Eloquent's collection methodspluck会对您有所帮助。假设您已为每个提到的表正确定义了模型,您可以这样做:

PlateContainer::find($id)
              ->container_slots
              ->pluck('occupied', 'id');

这将以下面的形式(基本上是一个数组)为您提供所需的预期结果:

=> Illuminate\Database\Eloquent\Collection {#1859
     all: [
       1 => true,
       2 => true,
       3 => false,
       4 => true
     ],
   }