0

My case like this

My code :

if($cover == 'on') 
    $photo = $image;

$this->product_repository->update($id, [
    'photo'     =>$photo,
    'photo_list'=>json_encode($photo_list)
]);

I want to add condition in array

If $cover != on then it not run this 'photo' => $photo

Seems it need to add conditon in array

How can I do it?

1
  • Do I understood correctly, that depending on $cover value you want to call update with either photo and photo_list or just with the photo_list? Prepare the array in separate variable adding elements as required, then pass it as a parameter to update method. Commented Jun 10, 2017 at 13:39

2 Answers 2

3

Create array with info for update and add photo key in condition:

$updateData = [
    'photo_list' => json_encode($photo_list)
];

if($cover == 'on'){
    $updateData['photo'] = $image;
}

$this->product_repository->update($id, $updateData);
Sign up to request clarification or add additional context in comments.

Comments

0

If photo is nullable you could do this:

$this->product_repository->update($id, [
    'photo'     => ($cover == 'on') ? $photo : null,
    'photo_list'=>json_encode($photo_list)
]);

2 Comments

If $cover != on, you answer will update photo field to be null. What I mean is $cover != on, it not update photo field
@samueltoh oh sure, you don't want to lose existing data. In that case I would go with Andrey's answer.

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.