【发布时间】:2020-10-15 12:37:05
【问题描述】:
我有物品目录管理系统。
目录项(型号):
Schema::create('catalog_items', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->float('price');
$table->float('deposit')->nullable();
$table->timestamps();
});
定价(型号):
Schema::create('pricings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name',127);
$table->boolean('enable')->default(true);
$table->timestamps();
});
CatalogItemPricingPivot(枢轴模型):
Schema::create('catalog_item_pricing', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('catalog_item_id');
$table->bigInteger('pricing_id');
$table->float('discount')->nullable()->default(0);
$table->timestamps();
});
现在,当我想更新 CatalogItem 的定价时,我会这样做:
$CatalogItem->pricings()->sync([
1 => array:1 [
"discount" => "25.00"
]
]);
这也可以
$CatalogItem->pricings()->sync([
1 => array:1 [
"discount" => "25.00"
],
5 => array:1 [
"discount" => "12.25"
]
]);
如果我在观察者中打印模型,我不会在原件中获得折扣字段
public function boot()
{
CatalogItemPricingPivot::updated(function($model){
dd($model);
});
}
结果
App\Pivots\CatalogItemPricingPivot {
table: "catalog_item_pricing"
exists: true
wasRecentlyCreated: false
attributes: array:4 [
"catalog_item_id" => 172
"pricing_id" => 1
"discount" => "25.00"
]
original: array:2 [
"catalog_item_id" => 172
"pricing_id" => 1
]
changes: array:2 [
"discount" => "25.00"
]
casts: []
}
它似乎改变了一点而不是。
如何在同步后获得带有额外字段的数据透视表的真正更改?
编辑:
MAIN 的目的是让 CatalogItemPricingPivot 在哪里更新
谢谢。
【问题讨论】:
标签: php laravel many-to-many pivot-table