Cloning relationships with ->replicate()
I'm using v2 and added the
->replicate
method. Works great. Making some changes in the beforeReplicaSaved
hook and all is well. I need to save the relationships. Is this just a matter of doing it the laravel way (per the docs) and just doing that in the after()
method or is there another way in Filament? Just wanted to check before I do this the hard way or something lol.5 Replies
I found the answer, Dan has already covered this. Has to be done manually.
His reply for reference: https://discord.com/channels/883083792112300104/883085291722788964/1011540053123661884
In case anyone tries this, in the
afterReplicaSaved
method, inject the $record (original) and $replica (copy). Then you can do the following. My model is called Tile
If you have a lot of relations (like I do) and have a lot of models you want to have duplication actions on (to where it would be a major pain to manually do all the record copying) you can also roll your own action, and use bkwld/cloner, which adds a duplicate() method to your model, which you can customize with the relations you do or don't want cloned.
Then in a custom action, just do ...
YourModel might look like ...
https://github.com/BKWLD/cloner
GitHub
GitHub - BKWLD/cloner: A trait for Laravel Eloquent models that let...
A trait for Laravel Eloquent models that lets you clone a model and it's relationships, including files. Even to another database. - GitHub - BKWLD/cloner: A trait for Laravel Eloquent mode...
The other useful thing I find with rolling my own duplicate action is when I need to override one or two fields on the duplicate. For example, here's where I let the user duplicate a DailyLog (for aircraft flights), but we know the flight_date needs to be changed ...
... which pops up a form with just "Flight Date", which then overrides the flight_date for the data that gets copied to the new record.
Very nice! I love what you cooked up here. I'm definitely adding this to my snippets collection and will use this pattern in the future. I like how clean this is! Well done.