简体   繁体   English

如何在laravel 5.2中克隆具有所有关系的雄辩的对象

[英]How to clone an eloquent object with all relations in laravel 5.2

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: 还有另一种关系'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 ? 如果我对您的理解不正确,请尝试以下操作: $clone = $order :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM