我使用Laravel 5.4开发了一个电子商务(具有多个产品属性功能)网站 . 一切都很好 . 但是当我尝试在数据透视表中同步同一属性的多个值时 . Laravel忽略了重复的削减 . 例如,我有一个名为“网络”的属性,它有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']
]);
任何建议,想法?
2 回答
Laravel中的
sync()
函数会自动读取重复项 . 你可以强迫它祝你好运伴侣!
使用sync方法使用以下关系在Controller中存储/更新数据: