How to Add EAV Attribute in Magento 2

In this topic, you will get an overview and clear instructions to Add EAV attribute in Magento 2.

Table of contents

To EAV (Entity Attribute Value) in Magento 2 is the core system when your business directly works with Magento 2 platform. Using EAV system allows auto-extending your models instead of there is an intervention into the database. In this topic, you will get an overview and clear instructions to set EAV attribute in Magento 2.

Overview of Magento 2 EAV attribute

Your Models will still extend \Magento\Framework\Model\AbstractModel.

Your Resource Models will extend \Magento\Eav\Model\Entity\AbstractEntity which implements the EntityInterface and DefaultAttributesProvider interfaces, extra methods compared to the standard \Magento\Framework\Model\ResourceModel\Db\AbstractDb are:

  • getAttribute('code') - get an attribute from the entity`

  • saveAttribute($this, 'code') - Save an attribute on an entity without going through the whole entities process

In the command to ask for your connections, you will use the __construct() method instead of the __init() method.

public function __construct(
        \Magento\Eav\Model\Entity\Context $context,
        $data = []
    ) {
        parent::__construct($context, $data);
        $this->setConnection('entity_type_code_read', 'entity_type_code_write');

In addition, applying the Magento\Eav\Model\Config class if you want to achieve the information related to the EAV attributes. The following methods will be considered to use:

  • getAttribute('entity_type_code', 'attribute_code')

  • getEntityType('entity_type_code')

Setting EAV attribute for product Magento 2

Create eavsetup factory

Run the following script and complete the creating EavSetup factory

     * @var EavSetupFactory
    protected $eavSetupFactory;
     * UpgradeData constructor
     * @param EavSetupFactory $eavSetupFactory
    public function __construct(EavSetupFactory $eavSetupFactory)
        $this->eavSetupFactory = $eavSetupFactory;

Add EAV attribute

/** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            * Add attributes to the eav/attribute
                    'group' => 'General',
                    'type' => 'int',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Is Featured',
                    'input' => 'boolean',
                    'class' => '',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '1',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => false,
                    'unique' => false,
                    'apply_to' => ''

Some characters need to be followed:

  • is_featured: enter attribute code
  • group: declare the group’s name in the backend
  • type: set type of the data
  • globar: define the scope of attribute (store, website or global)
  • visible_on_frontend: select true or false option to show the attribute on the frontend or not
  • apply_to: assign to the exact product type that you want to set attribute

Remove an eav attribute for product

You can also delete the eav attribute from product by running the command:

$entityTypeId = 4; // Find these in the eav_entity_type table
            $eavSetup->removeAttribute($entityTypeId, 'is_featured');

The topic is the basic and simple knowledge you need to know about the Entity Attribute Value (EAV) in Magento 2. If you still need any assistance about that, please comment below.

Enjoyed the tutorial? Spread it to your friends!


Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 EAV
  • magento 2 EAV attribute
  • magento 2 EAV tutorial
  • magento 2 EAV system
  • Add EAV attribute for product
  • magento 2 create attribute programmatically
  • magento 2 extension attributes
  • magento 2 add attribute
  • magento 2 add customer attribute
  • magento 2 product attribute programmatically
  • magento 2 add customer attribute programmatically
  • add eav attribute magento 2
  • magento 2 add new eav attribute
  • magento 2 add custom eav attribute
  • magento 2 add attribute to eav
  • 2.2.x, 2.3.x, 2.4.x