Magento 2 How To Load Model Data By Custom Field

Generally models in Magento 2 are loaded by the primary key field. But sometimes there are certain situations where we need to load a model by custom field. 

Load a Model Data by Custom Field Using following Code Snippet:

namespace Magemeta\Custom\Helper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Sales\Model\OrderFactory;
use Magemeta\Custom\Model\CustomFactory;
class Data extends AbstractHelper
    protected $orderCollectionFactory;
    protected $customFactory;
    public function __construct(
        Context $context,
        OrderFactory $orderCollectionFactory,
        CustomFactory $customFactory
        $this->orderCollectionFactory = $orderCollectionFactory;
        $this->customFactory = $customFactory;
    public function getCustomData()
        try {
            $quoteId = 10;  // this is test quoteId
            $collection = $this->orderCollectionFactory->create()->load($quoteId, 'quote_id');
            $customField = 'test';
            $customCollection = $this->customFactory->create()->load($customField, 'custom_field_name');
        } catch (\Exception $e) {

Hope this helps you Guys!!

Leave a Reply