0

Hi I have simple problem in my project. I want to update data in db. The sku codes sent with ajax. Like this A-1, A-2, A-3

I tried:

$sku2 = $request['sku'];
$sku = explode(',', $sku2);
foreach ($sku as $key => $value){
     $content = ProductNew::where('sku', $value)->update(['price' => 
     $price]);
}

But it updated only first record (A-1) Where is the problem?

This is full fuction

    public function setdiscount(Request $request)
    {
    $discount = Discount::where('id', $request['id'])->first();
    $d_price = $discount['price'];
    $d_type = $discount['type'];
    $price = 17;
    $sku2 = $request['sku'];
    $sku = explode(',', $sku2);
    foreach ($sku as $key => $value){
        ProductNew::whereIn('sku', $sku)->update(['price' => $price]);
    }



    } 

1 Answer 1

3

You can try using wherein() along with update() to update multiple records:

$sku2 = $request['sku'];
$sku = explode(',', $sku2);

$sku = array_map('trim', $sku);

ProductNew::whereIn('sku', $sku)->update(['price' => $price]);
Sign up to request clarification or add additional context in comments.

7 Comments

what's inside of $sku? is this an array with more than one value? check it: dd($sku);
array:5 [ 0 => "NHD-F070" 1 => " NHD-F070-1" 2 => " NHD-F070-2" 3 => " NHD-F070-3" 4 => " NHD-F070-4" ]
it seems to be no problem with this code, the issue might be somewhere else.
I notice there is space in other elements in your array. That is the issue. Look at this " NHD-F070-2" (there is a space at the start).
i added full function where the problem?
|

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.