Filament - Add an additional pivot field for many to many relationship.

Tables: 1. employees 2. expenses 3. employee_expenses (Pivot table) - employee_id - expense_id - amount_per_employee When I'm using the relationship manager; the emplyee_id and expense_id get saved in the employee_expenses table. But I want to store the amount_per_employee field also. I tried to do the below steps; i. mutateFormDataBeforeCreate method of Page Class ii. handleRecordCreation method iii. Used afterStateUpdated() and set amount_per_employee field in clouser along with that add amount_per_employee as a hidden field.
12 Replies
Dennis Koch
Dennis Koch2y ago
I am on the phone Right now. Check the docs. We have a section on pivot data.
charmykansagara
charmykansagaraOP2y ago
After thoroughly reviewing the documentation and diligently following each step, unfortunately, the solution does not seem to be effective in my particular situation.
Dennis Koch
Dennis Koch2y ago
Filament
Relation managers - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
charmykansagara
charmykansagaraOP2y ago
1. Yes I followed the same steps which have been provided in the doc. 2. You can find the model code in the attached SS.
Dennis Koch
Dennis Koch2y ago
Wait. Your field is hidden(). That means it won't be saved.
toeknee
toeknee2y ago
Yep as Dennis says, if you want a field to be saved but not shown, you need to use mutateBeforeSave()
charmykansagara
charmykansagaraOP2y ago
Hey @toeknee_iom @Dennis Koch Thank you so much for your help. But I am confused right now regarding the place where the mutateBeforeSave() method should be added. Can you please guide me as I'm new to filament; I don't have a major idea. Mild help from you will really save my day. FYI; The requirement is; I want to store the additional pivot field in the DB without displaying it on the form (You can consider it as hidden).
toeknee
toeknee2y ago
Is the pivot ID on the same record? And do you want it to be saved?
charmykansagara
charmykansagaraOP2y ago
Yes, The pivot id(employee_id) is being saved using relationship() method
toeknee
toeknee2y ago
See the above
charmykansagara
charmykansagaraOP17mo ago
I attempted the methods you suggested, but I'm still encountering the same issue. Hey @toeknee_iom @Dennis Koch Is there any other method that can solve my issue?
Want results from more Discord servers?
Add your server