2016-08-20 70 views
1

我想从现有雄辩模型及其所有关系中创建一个克隆,而不使用循环关系,因为我可能会在将来扩展关系。现在我有一个订单模式:如何克隆laravel中具有所有关系的雄辩对象5.2

$table->bigIncrements('id'); 
$table->unsignedBigInteger('customer_id'); 
$table->timestamps(); 

和秩序有很多项目:

$table->bigIncrements('id'); 
$table->unsignedBigInteger('order_id'); 
$table->unsignedInteger('quantity'); 

还有另一种关系“sell_orders:

$table->bigIncrements('id'); 
$table->unsignedBigInteger('order_id'); 

,但我知道它应该是超过这些在未来。我怎样才能从一个订单和所有的关系克隆?我发现这个代码:

$order = Order::find(1); 
$clone = $order->replicate(); 
$clone->push(); 
$clone->save(); 

这使得一个新的模型没有任何关系。

回答

1

您可以使用Observer模式与relationship.Refer Link

更新记录
0

如果我不介意你纠正错误,请尝试以下操作: $clone = $order? :d