Mutate pivot data on parent creation
Hello!
I have 2 entities called "Contact" and "Message". I added a pivot table between them using BelongsToMany. I also added pivot data with
->withPivot(['confirmation_code', 'confirmation_date', 'status'])
.
On my form, I only want to create a Message with some fields and a select in which I choose the Contacts (receivers).
The pivot data must not be modified by users, so I want to mutate the pivot data to generate a different 4-digit code for each Contact and initialize the status.
How can I achieve this?6 Replies
Anyone?
use ->after() and set the data manually?
After searching the docs, I found this part :
https://filamentphp.com/docs/3.x/panels/resources/creating-records#lifecycle-hooks
Do you think I should use afterCreate?
Another option is to create a Pivot model in Eloquent and use Observer there with method
creating()
, so automate it on Eloquent level and not Filament@Spârky I thought this situation would be beneficial for more people, so spent some time to write a tutorial: https://laraveldaily.com/post/filament-belongstomany-select-automatically-fill-pivot-values
Wow! Thanks a lot 🙂
I'm multitasking on a different project right now but I'll be sure to check this ASAP, thanks again and I'm marking this as closed