Skip to content

Instantly share code, notes, and snippets.

@jsdecena
Created March 13, 2018 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsdecena/5928e8c9892555704e93096b6c4401c8 to your computer and use it in GitHub Desktop.
Save jsdecena/5928e8c9892555704e93096b6c4401c8 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