Get Attribute Id By Attribute Code In Magento2
Get Attribute Id By Attribute Code In Magento2 – if you want to get the attribute id by attribute code.so here i am going to explain how to get the attribute id by attribute code. just you have to follow some steps.
1. Create constructor
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute
*/
protected $_eavAttribute;
/**
* @param Context $context
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute $eavAttribute
*/
public function __construct(
Context $context,
\Magento\Eav\Model\ResourceModel\Entity\Attribute $eavAttribute
)
{
$this->_eavAttribute = $eavAttribute;
parent::__construct($context);
}
2 . Call the getIdByCode method of Attribute in any method
$attributeId = $this->_eavAttribute ->getIdByCode(\Magento\Customer\Model\Customer::ENTITY, 'middlename');
In this way, you can get the attribute id by attribute code.
Categories:
Magento2
Tags:
attribute code attribute id attribute id by attribute code magento 2 attribute id by attribute code magento2 attribute id by customer attribute code how to get attribute id by attribute code in magento 2 Magento2
View Comments
Comment or Ask a Question
Quick Links