0

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?

7
  • Here you are having condition like where('id_branch', 'B')->where('id_item',$from_item->id_item) . is there any record available with same condition?? and which value becomes NULL?? Commented Aug 23, 2017 at 4:50
  • record is available e.g. branch A consist of 5 records while branch B consist of 2 records, when i am using updateOrCreate(), branch B will be added 3 records. Let's say there is another branch known as C and having the same record with branch B, when i want to update exactly same records with branch C to branch B, when i execute the code, the remarks all will set to null value. Commented Aug 23, 2017 at 4:56
  • Branch C - id_product - 7, remarks - abc while Branch B -id_product-7 remarks - cde, when i execute the code the Branch B remarks should be abc same as Branch C but it will set it as null value. Commented Aug 23, 2017 at 4:58
  • okay so you want to update id_branch B with the C , Right?? Commented Aug 23, 2017 at 5:17
  • Can say so, just like if some of the A record exist in B then B will be updated and add the record not existed in B from A. If C have exactly same record with B just with different remarks then only update the remarks in B from C. Commented Aug 23, 2017 at 5:25

1 Answer 1

1

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
        ]
    );
Sign up to request clarification or add additional context in comments.

5 Comments

is the id_branch and id_item are inside the $fillable array in Category model? and which attributes is saved as null?
yes, all attribute to be updated will set to default which is null. e.g remarks
There is other way to make it, but i am curious why updateOrCreate() not working for update the record. So, I try to find out for an answer haha.
Very strange, it should work fine, did you check if $from_item->id_item and $from_item->remarks has values?
Yes, after execute the code then all my value gone. I have no idea why also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.