Laravel Eloquent ORM replizieren
Ich habe ein Problem beim Replizieren eines meiner Modelle mit allen Beziehungen.
Die Datenbankstruktur sieht wie folgt aus:
Table1: products
id
name
Table2: product_options
id
product_id
option
Table3: categories
id
name
Pivot table: product_categories
product_id
category_id
Beziehungen sind:
product hasMany product_optionsProdukt gehört zu vielen Kategorien (durch product_categories)Ich möchte ein Produkt mit allen Beziehungen klonen. Zur Zeit ist hier mein Code:
$product = Product::with('options')->find($id);
$new_product = $product->replicate();
$new_product->push();
foreach($product->options as $option){
$new_option = $option->replicate();
$new_option->product_id = $new_product->id;
$new_option->push();
}
Aber das funktioniert nicht (die Beziehungen sind nicht geklont - momentan habe ich nur versucht, die product_options zu klonen).