2016-09-14 10 views
3

私はループをいくつかのjson値を設定しています。追加しようとした表示名のイムは、ここで私のダンプでlaravel set属性がjsonキーに追加されない

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

である私は、この

"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" 
     }, 

としてJSONを見ることができます

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; 

      } 
} 

しかし、私はのいずれかに変更しようとすると、この

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


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

私はちょうどこれを得る

"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" 
     }, 

カスタムフィールドのキーに追加する属性を設定するにはどうすればよいですか?

編集:

http://kopy.io/LQTDi

おかげで

を示唆するために@Manishする完全なファイル$製品 - > customfields - [ 'DISPLAY_NAME'] = $フィールド - > DISPLAY_NAME。今

"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" 
    }, 

を返しますが、カスタム表示名にサイズと色を持つ2つのフィールドがある

。私はおそらく、新しいループを作成し、display_nameをループする必要があると思う。この場合は助言してください。代わりにこれを行います。

+0

'Eloquent \ Model'クラスの' attributes'プロパティが保護されているので、実際にはうまく動作します。あなたは '過負荷のプロパティの間接的な変更'やそれに類するものを得るべきです。あなたはそのコードの中で何の誤りもないと確信していますか? – Skysplit

+0

エラーはオンです。しかし、私はOcotberCMSを使用しているので、多分実際には上記のコードと関係があります。 – ServerSideSkittles

+0

確かに。 OctoberCMSはプロパティの可視性を変更しますhttps://github.com/octobercms/library/blob/master/src/Database/Model.php#L48 – Skysplit

答えて

2

@ServerSideSkittlesあなたの意見が分かるかもしれません。私は間違っていたかもしれませんが、あなたの詳細によると、私はあなたがこれを必要と思う。

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; 
       } 
    } 

これを使用してください。この問題が解決されるかもしれません。

+0

これは素晴らしいです!私は私の質問を書き直していて、あなたはこの宝石を投稿しました。私は問題を説明するうまくやっていなかったので、ありがとう。 – ServerSideSkittles

関連する問題