I have this code in my Product model:
/**
* Get product's attributes.
*
* @return array
*/
public function attributes()
{
$nestedAttributes = [];
foreach ($this->attributeGroups as $attributeGroup)
{
print_r($attributeGroup);
if (isset($this->{$attributeGroup->code}))
{
dd($attributeGroup);
$attributes = json_decode($this->{$attributeGroup->code});
foreach ($attributes as $key => $attribute)
{
$nestedAttributes[ $key ] = $attribute;
}
}
}
return $nestedAttributes;
}
Then in my controller:
$product = Product::with('children')->find(9);
$product->attributes();
The problem was print_r($attributeGroup);
printed out the App\AttributeGroup Object
but dd($attributeGroup);
printed out a empty array. I don't know why it printed 2 difference values while it was just a variable name.