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.
Be the first to comment.