Back to Top

Get Attribute Id By Attribute Code In Magento2

Updated 4 January 2023

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.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home