I have a project in which I need to create 'n' number of duplicates of a record. This involves duplicating records in a child table. Right now i am INSERTing the duplicate parent record and then INSERT corresponding child records for that table.
My Parent table (portal_programs)
My Parent table (portal_programs)
try {
if ($programid) {
$programobj = Programs::find($programid);
if (!empty($eventobj)) {
$duplicateprogramobj = new Programs();
$duplicateprogramobj->title = $programobj->title;
.
.
.
$duplicateprogramobj->save();
$productsobj = AppModelsProduct::select('*')
->where('program_id','=',$programid)->get();
foreach($productsobj as $product)
{
$duplicateprod = new AppModelsProduct();
$duplicateprod->program_id = $duplicateprogramobj->id;
.
.
.
$duplicateprod->save();
}
}
}
} catch (Exception $ex) {
Log::critical($ex->getMessage());
}
Is there any better/efficient way to achieve the same?
question from:
https://stackoverflow.com/questions/65879657/laravel-8-x-best-way-to-create-duplicate-entries-in-mysql 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…