我在 Eloquent 中有多对多关系。
架构:
templates modules module_template
- id - id - module_id
- name - content - template_id
- order
但是,我需要使单个模板可以多次包含相同的模块。我该怎么做?
示例module_template
:
module_id | template_id | order
1 | 42 | 1
4 | 42 | 2
1 | 42 | 3
$template->modules()->detach($moduleId)
会 detach与模型的所有关系,对吗?假设甚至可以附加
它们。
最佳答案
您可以附加
多次,没有问题。但是 detach
将删除所有关联。 (当然,您的表不能应用 FK1,FK2
唯一/主要约束)
但是,不用担心:)您仍然可以手动完成:
$moduleId = 1;
$template->modules()
->newPivotStatementForId($moduleId)
->where('order', 3)
->delete()
关于php - Laravel 中同一模型的多个关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29917154/