Hospital Table
id | name | address
Hospital_Key Table
id | hospital_id(fk of Hospital Table) | type
Hospital_Column Table
id | hospital_id(fk of Hospital Table) | name
Hospital_fields Table
id | Hospital_Key_id(fk of Hospital_Key Table) | Hospital_col_id(fk of Hospital_Column Table) | data
I want to replicate/clone all data for any record and child records For example Hospital table for one record Get data from Hospital,Hospital_Key,Hospital_Column and replicate all data in the same table, with new primary keys.
$input = $request->all();
$library = Library::with('key', 'col','key.field')->findOrFail($input['lib_id']);
$new = $library->replicate();
$new->user_id = Auth::id();
$new->privacy_setting = 'Private';
$new->push();
$library->relations = [];
$library->load('key', 'col','key.field');
$relations = $library->getRelations();
foreach ($relations as $relation => $items) {
foreach ($items as $item) {
$newfield = $new->{$relation}()->create($item->toArray());
}
}
The above code does copy the records of Hospital,Hospital_Key,Hospital_Column The above code does not copy the records of Hospital_fields(grandchild)
Relations : Hospital hasmany Hospital_Key,Hospital hasmany Hospital_Column, Hospital_Key hasmany Hospital_fields How to replicate Hospital_fields Table data
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire