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
I am on the phone Right now. Check the docs. We have a section on pivot data.
After thoroughly reviewing the documentation and diligently following each step, unfortunately, the solution does not seem to be effective in my particular situation.
So you followed the steps in here?
https://filamentphp.com/docs/2.x/admin/resources/relation-managers#creating-with-pivot-attributes
Please show your model code
Filament
Relation managers - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
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.
Wait. Your field is
hidden()
. That means it won't be saved.Yep as Dennis says, if you want a field to be saved but not shown, you need to use mutateBeforeSave()
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).
Is the pivot ID on the same record? And do you want it to be saved?
Filament
Editing records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
Yes, The pivot id(employee_id) is being saved using relationship() method
See the above
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?