Replicate with relational data?

Is it possible to use the replicate function with replicating relationship data? I can write my own replicate action, but I am hoping there is a method with relationships or similar.
Solution:
i use this in my after to replicate my relation data. ```php ->after(function (Model $original, Model $replica): void { // Runs after the replica has been saved to the database....
Jump to solution
4 Replies
roni
roni5mo ago
Did you find anything?
toeknee
toeknee5mo ago
I just built it out myself in the end, It didn't seem possible out of the box
Dennis Koch
Dennis Koch5mo ago
You probably need to use the ->after() hook, if that's possible
Solution
Tieme
Tieme5mo ago
i use this in my after to replicate my relation data.
->after(function (Model $original, Model $replica): void {
// Runs after the replica has been saved to the database.
$OriginalLines = $original->offer_lines;
$ReplicaLines = collect();
$OriginalLines->each(function ($Line) use ($replica, $ReplicaLines) {
$ReplicaLines = $Line->replicate();
$ReplicaLines->offer_id = $replica->id;
$ReplicaLines->save();
$ReplicaLines->push($ReplicaLines);
});
})
->after(function (Model $original, Model $replica): void {
// Runs after the replica has been saved to the database.
$OriginalLines = $original->offer_lines;
$ReplicaLines = collect();
$OriginalLines->each(function ($Line) use ($replica, $ReplicaLines) {
$ReplicaLines = $Line->replicate();
$ReplicaLines->offer_id = $replica->id;
$ReplicaLines->save();
$ReplicaLines->push($ReplicaLines);
});
})