I want to clone my orders in order to let users re-order but I have orderstatus_id
column which is integer for getting status of orders.
The issue is if user clone completed
(any status) order cloned order will be the same status as old one, what I want is to get my orderstatus
id 1
which is waiting payment
in my case, so the cloned order status will be waiting payment
and not completed
. when I try $newOrder->orderstatus_id = 1;
I will get this error:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
here is my clone code:
public function reorder($id)
{
$order = Order::findOrFail($id);
$newOrder = $order->replicate();
$newOrder->orderstatus_id = 1;
$newOrder->save();
return redirect()->back()
->with('info',
'Order Stored');
}
any idea?
update
Orders
table schema
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
table schema
Schema::create('orderstatuses', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->unique();
$table->timestamps();
});