Skip to content

Instantly share code, notes, and snippets.

@ollieread
Forked from jsdecena/collection.php
Created March 13, 2018 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ollieread/d1c1bee58c7700ce3004db779626a419 to your computer and use it in GitHub Desktop.
Save ollieread/d1c1bee58c7700ce3004db779626a419 to your computer and use it in GitHub Desktop.
<?php
$attributes = $productAttributes
->pluck('attributesValues')
->flatten()
->unique()
->groupBy(function (AttributeValue $av) {
return $av->attribute->name;
})
->map(function (Collection $collection) {
return $collection->map(function (AttributeValue $av) {
return $av->value;
})->unique();
})
->all();
dump($attributes);
// returns
array:2 [▼
"Color" => Collection {#574 ▼
#items: array:2 [▼
0 => "red"
6 => "blue"
]
}
"Size" => Collection {#565 ▼
#items: array:3 [▼
0 => "small"
1 => "medium"
2 => "large"
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment