dan*_*oli 5 php many-to-many laravel laravel-5.4
我使用了很多很多关系.我想为此属性设置两个值.
产品,
属性
attribute_product => product_id,attribute_id,value
我知道这是错的,但我想告诉你我想要的
$product->attributes()->sync([
1 => [
'value' => 'sky'
],
1 => [
'value' => 'night'
],
]);
Run Code Online (Sandbox Code Playgroud)
更新2
Schema::create('attribute_product', function (Blueprint $table) {
$table->unsignedInteger('product_id');
$table->unsignedInteger('attribute_id');
$table->text('value')->nullable();
$table->integer('devalue_id')->nullable(); // value id
$table->primary(['product_id', 'attribute_id', 'devalue_id']);
});
Run Code Online (Sandbox Code Playgroud)
更新1
我需要设置天空,晚上
product_id attribute_id value devalue_id
1 1 sky 1
1 1 night 2
...
Run Code Online (Sandbox Code Playgroud)
我不是 100% 确定你想做什么,所以我将解决两种不同的情况。
1:创建属性并将其链接到产品。在本例中,您有一个产品并且知道属性名称,但它还不在您的属性表中。它看起来像:
$product->attributes()->saveMany([
new App\Attribute(['title' => 'sky']),
new App\Attribute(['title' => 'night']),
]);
Run Code Online (Sandbox Code Playgroud)
2:将现有属性附加到产品。在这种情况下,您的属性表中已有该属性并且已对其进行了查找。实际上,除了查找属性之外,它非常相似。
$attribute1 = Attributes::where('title', 'sky')->first();
$attribute2 = Attributes::where('title', 'night')->first();
$product->attributes()->saveMany([
$attribute1,
$attribute2,
]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
692 次 |
| 最近记录: |