Search code examples
phpjsonlaraveleloquentoctobercms

laravel set attribute won't append to json key


I have a loop setting some json values.

 if (isset($product->customfields)) {
            foreach ($product->customfields as $customfield) {
                $fieldId = $customfield['custom_field_id'];
                // Grab custom field template code
                $field = CustomField::find($fieldId);

                $product->attributes['display_name'] = $field->display_name;

            }
}

Where the display name im trying to add is here

$product->attributes['display_name'] = $field->display_name;

In my dump I can see the json as this

 "id":"20",
 "display_name":"Size",
   "customfields":[  
      {  
         "id":"1",
         "product_id":"20",
         "store_id":null,
         "custom_field_id":"1",
         "value":"RED",
         "created_at":"2016-09-14 09:32:00",
         "updated_at":"2016-09-14 09:32:14"
      },

But when I try to change to EITHER of this

 $product->attributes['customfields']['display_name'] = $field->display_name;


 $product->attributes['customfields'] = $field->display_name;

I just get this

"id":"20",
"customfields":[  
      {  
         "id":"1",
         "product_id":"20",
         "store_id":null,
         "custom_field_id":"1",
         "value":"RED",
         "created_at":"2016-09-14 09:32:00",
         "updated_at":"2016-09-14 09:32:14"
      },

How do I set the attribute to add to the customfields key?

Edit:

The full file http://kopy.io/LQTDi

thanks to @Manish for suggesting

$product->customfields-['display_name'] = $field->display_name;

which now returns

"customfields":{  
      "0":{  
         "id":"1",
         "product_id":"10000",
         "store_id":null,
         "custom_field_id":"1",
         "value":"RED",
         "created_at":"2016-09-14 09:32:00",
         "updated_at":"2016-09-14 09:32:14"
      },
      "1":{  
         "id":"2",
         "product_id":"10000",
         "store_id":null,
         "custom_field_id":"2",
         "value":"",
         "created_at":"2016-09-14 10:22:14",
         "updated_at":"2016-09-14 10:22:14"
      },
      "display_name":"Size"
   },

But in the custom display name there are two fields with Size and Color. I think I maybe need to make a new loop and loop the display_name through. If this is the case then please advise and I will do this instead.


Solution

  • @ServerSideSkittles May be i got your point. May be i was wrong but as per your details i think you need this.

    if (isset($product->customfields)) {
    
                    foreach ($product->customfields as $customfieldKey => $customfieldVal) {
                        $fieldId = $customfieldVal['custom_field_id'];
                        // Grab custom field template code
                        $field = CustomField::find($fieldId);
    
                        $product->customfields[$customfieldKey]['display_name'] = $field->display_name;
                    }
        }
    

    Try use this. May be this will sort out the issue.