Relation field not saving in pivot table #1724
-
Is the relation field supposed to save when you have an relationship table? I have a relationship field setup on my employee edit screen:
The dropdown correctly lists all the location entries but doesn't seem to save. I have a database pivot table called "location_employee" with columns for location_id and employee_id. In my Employee model I have:
Is this supposed to automatically save in the pivot table (and I have something incorrect here) or do I have to create my own code for saving data in a pivot table? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 12 replies
-
In case anyone has this challenge in the future, Here is how I got my data saving and pulling from the pivot table. In my Employee edit screen file I needed to add the following to the createOrUpdate method after the initial save:
In the same file where I define my form fields I used:
In the same file in my query method I added the following before the return. This loads my pivot table data to populate the form field with the store value(s).
In my Model file I have the relationship defined like this:
All of this allowed me to save the data from the form field and show the saved value. Hope this helps someone else in the future. |
Beta Was this translation helpful? Give feedback.
-
Thank you, due to you my problem is solved!!! I just had a little trouble understanding that |
Beta Was this translation helpful? Give feedback.
-
I've been struggling with the same issue for a while now. I'm trying to relate Products to Products in a many-to-many relationship via a pivot table called I've defined this relationship in my Product model as:
I'm using Orchid CRUD and my ProductResource file has the following relation fields:
They're both trying to do the same thing (display and update related products) but both fail in different ways:
I need a field that does both! My ProductResource file has the following functions:
Any clues? |
Beta Was this translation helpful? Give feedback.
-
@tabuna My solution was to add one line to the save method: I think a line like this should be added to the documentation. (Or are we wrong and the relation field should indeed be updated automatically, and there is a bug somewhere prohibiting this?) |
Beta Was this translation helpful? Give feedback.
In case anyone has this challenge in the future, Here is how I got my data saving and pulling from the pivot table.
In my Employee edit screen file I needed to add the following to the createOrUpdate method after the initial save:
$employee->locations($employee->id)->sync(optional($request->get('employee'))['locations']);
In the same file where I define my form fields I used:
In the same file in my query method I added the following b…