Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在laravel 5.2中克隆具有所有关系的雄辩对象_Php_Laravel_Activerecord_Laravel 5_Eloquent - Fatal编程技术网

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

Php 如何在laravel 5.2中克隆具有所有关系的雄辩对象,php,laravel,activerecord,laravel-5,eloquent,Php,Laravel,Activerecord,Laravel 5,Eloquent,我想从现有的雄辩模型及其所有关系中复制一个,而不使用关系上的循环,因为我将来可能会扩展关系。目前,我有一个订单模型: $table->bigIncrements('id'); $table->unsignedBigInteger('customer_id'); $table->timestamps(); 订单中有很多项目: $table->bigIncrements('id'); $table->unsignedBigInteger('order_id'); $t

我想从现有的雄辩模型及其所有关系中复制一个,而不使用关系上的循环,因为我将来可能会扩展关系。目前,我有一个订单模型:

$table->bigIncrements('id');
$table->unsignedBigInteger('customer_id');
$table->timestamps();
订单中有很多项目:

$table->bigIncrements('id');
$table->unsignedBigInteger('order_id');
$table->unsignedInteger('quantity');
还有另一个关系的销售订单:

$table->bigIncrements('id');
$table->unsignedBigInteger('order_id');
但我知道将来应该不止这些。如何从订单及其所有关系复制?我发现这个代码:

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

这将创建一个没有任何关系的新模型。

如果我不理解u的正确性,请尝试以下操作:
$clone=$order

:D

您可以使用观察者模式来更新具有关系的记录。请参阅