F
Filament13mo ago
toeknee

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
rajul
rajul10mo ago
Did you find anything?
toeknee
toekneeOP9mo ago
I just built it out myself in the end, It didn't seem possible out of the box
Dennis Koch
Dennis Koch9mo ago
You probably need to use the ->after() hook, if that's possible
Solution
Tieme
Tieme9mo 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);
});
})
Want results from more Discord servers?
Add your server