Reputation: 867
id_branch is PK, id_item is PK&FK
$id = B;
$id_selected = A;
$from_category= Category::where('id_branch', $id_selected)->get();
foreach ($from_items as $from_item) {
$test = Category::updateOrCreate(['id_branch' => $id,'id_item'=>$from_item->id_item], ['remarks' => $from_item->remarks]);
}
For example, user will select which branch category record need to be copied to the current branch category. After that will update or insert the record from the branch category selected to current one. Copy branch category A record to branch category B. If exist then update else insert. I able to insert the record but when update the value will be null. Anything wrong with my code?
Upvotes: 0
Views: 3092
Reputation: 717
updateOrCreate
method take 2 parameters the first one is the conditions and the second one is the data to update or create.
In your case if the ID
not exists in the branch, it will create a new row with the given data but you didn't pass the id_branch
and id_item
in the second parameter, I guess to fix this issue you should write the code like the following
$test = Category::updateOrCreate(
[
'id_branch' => $id,
'id_item'=>$from_item->id_item
],
[
'id_branch' => $id,
'id_item'=>$from_item->id_item,
'remarks' => $from_item->remarks
]
);
Upvotes: 1