I created a custom attribute for customer and would like to filter my collection through it.
My UpgradeData:
if (version_compare($context->getVersion(), '1.0.6', '<')) {
$attributesInfo = [
'celular' => [
'label' => 'Celular',
'type' => 'varchar', // can be static too
'input' => 'text',
'unique' => true,
'position' => 90,
'visible' => true,
'required' => true,
'system' => 0, // Important otherwise attribute is not seen as custom attribute
],
];
foreach ($attributesInfo as $attributeCode => $attributeParams) {
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $attributeCode, $attributeParams);
}
foreach ($attributesInfo as $attributeCode => $attributeParams) {
$attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $attributeCode);
$attribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$attribute->save();
}
}
My Collection:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection');
$customer = $customerObj->addAttributeToSelect('*')
->addAttributeToFilter('celular',$username)
->load()->getData();
Even though I am attaching a value to this field, no customer is found, what should I do?
--EDIT!