-
-
Save grim-reapper/eed2e18327a5594fb335b4085119abe5 to your computer and use it in GitHub Desktop.
Replicate (Duplicate) Eloquent Model With Relations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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