{"id":1138,"date":"2020-07-24T04:51:47","date_gmt":"2020-07-24T04:51:47","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/07\/24\/load-model-data-by-custom-field-magento-2\/"},"modified":"2025-05-22T16:50:20","modified_gmt":"2025-05-22T11:20:20","slug":"load-model-data-by-custom-field-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/load-model-data-by-custom-field-magento-2\/","title":{"rendered":"How to Load Model Data By Custom Field in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In most instances, Magento 2 developers use Model to load records. Generally, models in Magento 2 are loaded by the field with the primary key.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the time of client\u2019s requirements where they want to load model data using a non-primary column field, you can use the programmatic method below to load model data by custom field in Magento2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generally, we use <em>addFieldToFilter<\/em> on collection and <em>getFirstItem<\/em> or loop over the collection. Though, we can use the model to load data based on any custom column.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let us understand with an example. If you are working with a custom model and you don\u2019t have an ID or any other data of the customer, but you have to load the model data by other custom fields like email_id or quote_id. Here\u2019s the solution to load model data by custom field in Magento2!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Programmatic Method to Load Model Data by Custom Field<\/strong><\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace [Vendor]\\[Module]\\Helper;\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Sales\\Model\\OrderFactory;\nuse [Vendor]\\[Module]\\Model\\CustomFactory;\nclass Data extends AbstractHelper\n{\n    protected $orderCollectionFactory;\n    protected $customFactory;\n    public function __construct(\n        Context $context,\n        OrderFactory $orderCollectionFactory,\n        CustomFactory $customFactory\n    )\n    {\n        $this->orderCollectionFactory = $orderCollectionFactory;\n        $this->customFactory = $customFactory;\n        parent::__construct($context);\n    }\n    public function getCustomData()\n    {\n        try {\n            $quoteID = 11;  \/\/ you can change quoteID\n            $collection = $this->orderCollectionFactory->create()->load($quoteID, 'quote_id');\n            \/\/ custom model example\n            $custom_id = 5;\n            $customCollection = $this->customFactory->create()->load($custom_id, 'custom_id');\n        } catch (\\Exception $e) {\n            $this->_logger->info($e->getMessage());\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also read:<\/strong> <a href=\"https:\/\/meetanshi.com\/blog\/alternative-to-magento-2-deprecated-load-save-and-delete-methods\/\" target=\"_blank\" rel=\"noreferrer noopener\">Alternative to Magento 2 Deprecated Load, Save and Delete Methods<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do share the post with the Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In most instances, Magento 2 developers use Model to load records. Generally, models in Magento 2 are loaded by the field with the primary key.&nbsp;&#8230;<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1138","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1138"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1138\/revisions"}],"predecessor-version":[{"id":15416,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1138\/revisions\/15416"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}