Magento 2 – How to Get Attribute Options Value of EAV Entity

databaseeavmagento2

How can I get the attribute options values of eav entity?
I found solution only for magento 1.x but M2 I don't know.
M1:

$attr = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('specialty')->getData()[0];
$attributeModel = Mage::getModel('eav/entity_attribute')->load($attr['attribute_id']);
$src =  $attributeModel->getSource()->getAllOptions();

Anyone know, show me step by step, pls!Thanks!

Best Answer

you can add to the constructor of your class an instance of \Magento\Eav\Model\Config like this:

protected $eavConfig;
public function __construct(
    ...
    \Magento\Eav\Model\Config $eavConfig,
    ...
){
    ...
    $this->eavConfig = $eavConfig;
    ...
}

then you can use that in your class

$attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute_code_here');
$options = $attribute->getSource()->getAllOptions();
Related Topic