dimanche 21 juin 2020

can i modify laravel collection key value like we do in php arrays?

for eg:-

foreach ($data as $key => $value) {

            $new_data[$value->attribute_id]['options'][$value->id] = $value->name; 
        }

Indirect modification of overloaded element of App\Attribute has no effect gives me this error.

although i solve it by using this code but i still need to understand if I can modify collection like directly pointing towards key like we do in array even if it is a model object in it.

return collect($new_data)->transform(function ($value) use ($data){
         $value->options =  $data->reduce(function ($newattr_value,$attr_value) use($value) {
                if($attr_value['attribute_id'] == $value->id){
                    $newattr_value[$attr_value['id']] = $attr_value['name'];
                }
                return $newattr_value; 
            },[]); 
         return $value;
       });


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire