Magento 2.3 – How to Get Customer Attribute Value by Customer ID

backendcustomercustomer-attributegridmagento2.3

I have customer entity id, how do i get customer attribute value by loading customer entity id, Please provide me a solution

Best Answer

you can use this code to get customer attribute value.

class MyClass 
    {
        protected $_customer;
        protected $_customerFactory;

        public function __construct(...
               \Magento\Customer\Model\CustomerFactory $customerFactory,
               \Magento\Customer\Model\Customer $customers
        )
        {
            ...
            $this->_customerFactory = $customerFactory;
            $this->_customer = $customers;
        }

        public function getCustomerCollection() {
            return $this->_customer->getCollection()
                   ->addAttributeToSelect("*")
                   ->load();
        }

        public function getFilteredCustomerCollection() {
            return $this->_customerFactory->create()->getCollection()
                    ->addAttributeToSelect("*")
                    ->addAttributeToFilter("firstname", array("eq" => "Max"))
                    -load();
        }
    }
Related Topic