Skip to content

Instantly share code, notes, and snippets.

@grim-reapper
Forked from jnbn/controller.php
Created June 23, 2020 07:08
Show Gist options
  • Select an option

  • Save grim-reapper/eed2e18327a5594fb335b4085119abe5 to your computer and use it in GitHub Desktop.

Select an option

Save grim-reapper/eed2e18327a5594fb335b4085119abe5 to your computer and use it in GitHub Desktop.
Replicate (Duplicate) Eloquent Model With Relations
<?php
public function replicateWithRelations(QuestionCategory $questioncategory)
{
$newCategory = $questioncategory->replicate();
$newCategory->name = "Kopyası: ".$questioncategory->name;
$newCategory->push();
$questioncategory->relations = [];
//load relations on EXISTING MODEL
$questioncategory->load('questions');
//re-sync everything
foreach ($questioncategory->getRelations() as $relationName => $values){
$newCategory->{$relationName}()->sync($values);
}
return redirect()->route('neon.questioncategory.edit',$newCategory->id)->withSuccess("Kategori başarıyla kopyalandı");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment