【发布时间】:2020-03-31 22:15:41
【问题描述】:
我们有可以通过 mysql 创建的自定义字段,这些字段由模块“拥有”(例如:机会模块想要一个“潜在销售”字段,用户部分想要一个性别字段(有 3选择选项)。
使用 Laravel 6 Eloquent,我正在尝试获取:
- 所有机会
- 和 custom_fields where category = 'opportunity'
- 和相关的 custom_fields_values 通过数据透视。
然后将它们添加到集合中的“相关”数组中。所以我可以轻松做到:
$opportunity->custom_field[x]->custom_field->name
$opportunity->custom_field[x]->custom_field_value[x]->value
数据库:
custom_fields:
id, name, category (opportunity, client...), type (text, number, select, radio)
custom_field_custom_field_value (pivot):
custom_field_id, custom_field_value_id, parent_id (=id of the "parent" opportunity or client...), selected
custom_field_values:
id, value
我很想自己开始,但我 100% 被卡住了 - 甚至不知道从哪里开始使用 belongstomanys 等等......或者它是否完全可行......
【问题讨论】:
标签: php mysql laravel join eloquent