我有 TableSale
和Product
那个链接many-to-many
。
在Product Schema
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
...
$table->double('price');
....
});
in product_sale Schema
Schema::create('product_sale', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('sale_id')->nullable();
$table->unsignedBigInteger('product_id')->nullable();
$table->integer('quantity')->default(1);
$table->double('unit_price');
$table->integer('discount')->nullable()->default(0);
$table->timestamps();
$table->foreign('sale_id')->references('id')->on('sales')->onDelete('set null');
$table->foreign('product_id')->references('id')->on('products')->onDelete('set null');
});
当然,我想price
从产品中查询以保存到unit_price
表product_sale
中..
我想知道,有没有办法可以存档?
And Here in SaleController
if(isset($request->items)) {
foreach($request->items as $item) {
$sale->products()->attach($item['id'], [
'unit_price' => $item['unit_price'],
'quantity' => $item['quantity'],
'discount' => $item['discount'],
]);
}
}
提前致谢...