我想克隆我的订单,以便让用户重新订购,但我有orderstatus_id
列,这是获取订单状态的整数。
问题是,如果用户克隆completed
(任何状态)订单克隆订单的状态与旧订单相同,我想要的是获取orderstatus
ID 1
,这是在我的情况下,waiting payment
,因此克隆订单状态为waiting payment
而不是completed
。当我尝试$newOrder->orderstatus_id = 1;
时,我会收到此错误:
完整性约束违规:1452无法添加或更新子行: 外键约束失败
这是我的克隆代码:
public function reorder($id)
{
$order = Order::findOrFail($id);
$newOrder = $order->replicate();
$newOrder->orderstatus_id = 1;
$newOrder->save();
return redirect()->back()
->with('info',
'Order Stored');
}
任何想法?
Orders
表架构
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->text('cart');
$table->integer('orderstatus_id')->unsigned();
$table->integer('address_id')->unsigned();
$table->string('payment_id');
$table->timestamps();
});
Schema::table('orders', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('address_id')->references('id')->on('addresses');
$table->foreign('orderstatus_id')->references('id')->on('orderstatuses');
});
orderstatuses
表架构
Schema::create('orderstatuses', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->unique();
$table->timestamps();
});