I'm trying to create a new row if there isn't one yet with the following condition:
['employee_id' => $id, 'competence_id' => $getCompetenceKey]
or if it exists already, I update it and set ['value' => $getCompetenceValue]
So I did this:
foreach($request->input('competence') as $getCompetenceKey => $getCompetenceValue) {
EmployeeCompetence::updateOrCreate(
['employee_id' => $id,'competence_id' => $getCompetenceKey],
['value' => $getCompetenceValue]
);
}
But it doesn't seem to work properly:
https://i.sstatic.net/8ndQt.png
I could do it like this though:
foreach ($request->input('competence') as $getCompetenceKey => $getCompetenceValue) {
$e = EmployeeCompetence::where('employee_id', $id)->where('competence_id', $getCompetenceKey);
if ($e->count() > 0) {
$e->update(['value' => $getCompetenceValue]);
} else {
EmployeeCompetence::create(['employee_id' => $id,
'competence_id' => $getCompetenceKey,
'value' => $getCompetenceValue]);
}
}
But I really want to know why the updateOrCreate() function didn't work for me.
Thanks,
Kenny