Update only one Field in Structure without setting other values?

I am trying to update my page’s structure, it’s basically deleting every value i don’t set. While i intend to update only one value out of the whole structure…


      $product->update([
        'availability' => yaml::encode($stock),
      ],'de');

while availability has diffrent fields such as size, price and stock, and obviously diffrent languages, where there might be diffrent values for all other structure-fields other than for the stock one ( stock field is supposed to be the same value for all language versions )

Is there any smart way to get it done without much hassle?

array_map() is probably what you are looking for.

thanks, you pointed me in the right direction and at least for some dirty test could get it done. just need to refine the code.

    $default = array_map(null, $product->availability()->yaml());
        foreach($default as $array){
      if($array['size'] == $product->availability()->toStructure()->filterBy('size',$size)->nth(0)->size()->value()){
        $newstock = $currentstock-$qty;
        $stock[] = [
          'stock' => $newstock,
          'size' => $array['size'],
          'price' => $array['price'],
          'maxstock' => $array['maxstock'],
        ];
      } else {
        $stock[] = [
          'stock' => $array['stock'],
          'size' => $array['size'],
          'price' => $array['price'],
          'maxstock' => $array['maxstock'],
        ];
      }
    }
    $product->update([
      'availability' => yaml::encode($stock),
    ],'de');
1 Like