我使用 Laravel 5.4 开发了一个电子商务(具有多个产品属性功能)网站。一切正常。但是当我尝试在数据透视表中同步同一属性的多个值时。Laravel 会忽略重复的部分。例如,我有一个名为“Network”的属性,它有 3 个值:2G、3G、4G。一部手机支持3G和4G网络。我想在数据库中同步 3G 和 4G 值。Laravel 忽略了其中一个。
Products Table:
ID - Product Name
1 - Test Mobile
Attributes Table
ID - AttributeName
1 - Network
AttributeValues Table
ID - AttributeID - AttributeValue
1 - 1 - 2G
2 - 1 - 3G
3 - 1 - 4G
ProductAttributes Table
ID - AttributeID - ProductID - AttributeValue
1 - 1 - 1 - 3G
1 - 1 - 1 - 4G
我想将产品属性存储在类似的“ProductAttributes”表中。但是 Laravel 忽略了其中之一。
我正在保存这样的数据:
$product = Product::create([
'name' => 'Test Mobile'
]);
$product->attributes()->sync([
1 => ['AttributeValue' => '3G'],
1 => ['AttributeValue' => '4G']
]);
任何建议,想法?