4

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.

ServerSideSkittles
  • 2,713
  • 10
  • 34
  • 60
  • I am pretty surprised that it actually even works, as `attributes` property in `Eloquent\Model` class is protected. You should get error `Indirect modification of overloaded property` or something like that. Are you sure you're not supressing any erros within that code? – Skysplit Sep 14 '16 at 11:58
  • Errors are on. However I am using OcotberCMS, so maybe it has something to do with the above code actually working. – ServerSideSkittles Sep 14 '16 at 12:06
  • Indeed. OctoberCMS alters property visibility https://github.com/octobercms/library/blob/master/src/Database/Model.php#L48 – Skysplit Sep 14 '16 at 12:12
  • Did you try like this `$product->customfields['display_name'] = $field->display_name;` – Manish Sep 14 '16 at 12:14
  • @Manish That somewhat works, it adds display name inside customfields but not in the id "1". When I have multiple customvalues it will go customfields: [ { "id":1 }, {"id":2}, etc. How would I loop each display_name inside each object? – ServerSideSkittles Sep 14 '16 at 12:24
  • Could you explain more regarding this with proper example? – Manish Sep 14 '16 at 12:26

2 Answers2

2

@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.

Manish
  • 3,443
  • 1
  • 21
  • 24
  • This is amazing! I was reediting my question and you posted this gem. Thankyou very much as I was not doing a good job of explaining the issue. – ServerSideSkittles Sep 14 '16 at 12:42
0

I know this is old, but does this answer your question?

$product->setAttribute ('customfields->display_name', 'Display name of my column');

However, taking a step back, and circumventing entirely the point of your question, one could argue that you don't really want to hard-code the display name in your database model at all. Display names should be something that's managed in your translation/localisation file.

In your Blade template file, try:

{{ __($my_field_id) }}

And then you would have a translation for your field-id in the localisation file.

cartbeforehorse
  • 3,045
  • 1
  • 34
  • 49