Back to Top

How to load Model by another field in Magento 2

Updated 1 March 2024

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.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

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