I’m doing a dental function, I’m looking to sync data in a many-to-many
relationship. I have the following:
tooth
- id
- name
cost
- id
- name
cost_tooth
- id
- tooth_id
- cost_id
- row_number
This is the data I got from input checkbox tooth_id
, I have an array like this
[
0 => [
1 => ["row_number" => 1]
2 => ["row_number" => 1]
3 => ["row_number" => 1]
4 => ["row_number" => 1]
]
1 => [
1 => ["row_number" => 2]
2 => ["row_number" => 2]
3 => ["row_number" => 2]
4 => ["row_number" => 2]
]
]
my code
foreach ($output as $key => $value) {
$cost_id->tooths()->sync($value);
}
I only get the last data but not all the data.
I know attach
method can solve for me but I want to try with sync
method
2
Answers
I solved my problem, here is my code
Edit
But I still have a problem, it's updating
Because you use sync in loop
if you want update version
if you want store version