Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5488

How to load Model by another field in Magento 2

$
0
0

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('test@webkul.com','email');
    .....
}

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


Viewing all articles
Browse latest Browse all 5488

Trending Articles