I want to make a clone from existing eloquent model with all its relations without using loops on relations because i may extend relations in future. For now i have an order model:
$table->bigIncrements('id');
$table->unsignedBigInteger('customer_id');
$table->timestamps();
and order has many items:
$table->bigIncrements('id');
$table->unsignedBigInteger('order_id');
$table->unsignedInteger('quantity');
there is another relations 'sell_orders:
$table->bigIncrements('id');
$table->unsignedBigInteger('order_id');
but i know it should be more than these in future. How can i make a clone from an order and all its relations? I found this code:
$order = Order::find(1);
$clone = $order->replicate();
$clone->push();
$clone->save();
This make a new model without any relations.
您可以使用观察者模式来更新具有关系的记录.Refer Link
If i unterstand u correct, try the following: $clone = $order
? :D
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.