How to add default values to a multiselect with a belongstomany relation?
I have a user-rental pivot table and when creating a new rental I would need to assign the users that are active, active = 1
4 Replies
maybe use an observer on the pivot model
Thank you Dan Darrin. I have tried but I can't get it to work with the created method when creating a new record, on the other hand, if I put the same code in udpated it does work when creating a new record.
I can see the log that is inside the if, but nothing save in DB
observer on the pivot model, not the related model
because the pivot model should be listened to when something gets attached
Thanks, I understand you. But from the resource form I am not adding anything in the pivot table, only in the related. What I want is that when I add a new record in Rental, that rental automatically assigned in the pivot table to the active users. Maybe I am missing something, but I don't quite understand how to do it as you say, because if I put the observer in the pivot table it won't launch because I don't add anything, right?
I active MySQL log and and I see that it does the insert but followed by a delete. but I don't have any delete configured:
I have found the "problem". In the Resource, I have a multiselect to add/edit users and it deleted the records once the observer added it. At the moment I have eliminated the multiselect until I find a solution.