I am using Magento 2.1.5, and created custom attribute for customer and trying to get & set the values of custom attribute but didn't get custom attribute in customer collection and as well as set also.
add custom attribute:
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'sample_attribute');
$eavSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'sample_attribute',
[
'type' => 'int',
'label' => 'Sample Attribute',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'required' => true,
'default' => '0',
'sort_order' => 100,
'system' => false,
'position' => 100
]
);
$sampleAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'sample_attribute');
$sampleAttribute->setData(
'used_in_forms',
['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
);
$sampleAttribute->save();
Get custom attribute value :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->create('Magento\Customer\Model\Customer')->load('1');
echo "<pre>"; print_r($customerRepository->getData());