Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DEV

How to load Model by another field in Magento 2

In this blog I will explain how you can load model by another custom field in Magento 2. Mostly we use addFieldToFilter on collection and getFirstItem or loop over the collection. However we can use the model to load based on some custom column.

In the below example we have loaded the Student model based on email column.

public function __construct(
    .....
    \Vendor\ModuleName\Model\StudentFactory $studentFactory,
    .....
) {
    .....
    $this->studentFactory = $studentFactory;
    .....
}
 
public function execute()
{
    .....
    $student = $this->studentFactory->create()->load('[email protected]','email');
    .....
}

Thanks for reading. Feel free to comment if you face any issue.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • Hiep Mai
    • Sanjay Chouhan (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home