How to get Magento 2 customer collection?
UsingFactory method
class CustomerCollection
{
protected $_customerFactory;
public function __construct(
...
\Magento\Customer\Model\CustomerFactory $customerFactory,
...
)
{
...
$this->_customerFactory = $customerFactory;
...
}
public function getFilteredCustomerCollection() {
return $this->_customerFactory->create()->getCollection()
->addAttributeToSelect("*")
-load();
}
}
Using Model
class CustomerCollection
{
protected $_customer;
public function __construct(
...
\Magento\Customer\Model\Customer $customers
...
)
{
...
$this->_customer = $customers;
...
}
public function getCustomerCollection() {
return $this->_customer->getCollection()
->addAttributeToSelect("*")
->load();
}
}
Add Filter in collection
$firstName = 'saphal';
return $this->_customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter("firstname", array("eq" => $firstName))
-load();