F
Filament12mo ago
RT2023

Clone record with relationship table

Hi all, I have an application with orders and orderitems models. I want to duplicate an order using the replicate action from the table view. When I do that, I cant replicate the data from the order table but it doesnt replicate the data from the orderitems table. Is there a way to do this? a plug in or something similar? Thanks for your help
2 Replies
Lara Zeus
Lara Zeus12mo ago
maybe something like this
->beforeReplicaSaved(function (Order $replica, Order $record, array $data): void {
$record->items->each(function ($item) use ($orderID) {
$orderItem = $item->replicate()->fill(['order_id' => $orderID]);
$orderItem->save();
});
})
->beforeReplicaSaved(function (Order $replica, Order $record, array $data): void {
$record->items->each(function ($item) use ($orderID) {
$orderItem = $item->replicate()->fill(['order_id' => $orderID]);
$orderItem->save();
});
})
for more https://filamentphp.com/docs/3.x/actions/prebuilt-actions/replicate#lifecycle-hooks
RT2023
RT202312mo ago
Thanks for the info. Will try this solution and follow up