{"id":1008,"date":"2020-05-18T12:31:45","date_gmt":"2020-05-18T12:31:45","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/05\/18\/get-data-from-custom-database-table-in-magento-2\/"},"modified":"2025-05-22T13:04:08","modified_gmt":"2025-05-22T07:34:08","slug":"get-data-from-custom-database-table-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-data-from-custom-database-table-in-magento-2\/","title":{"rendered":"How to Get Data From Custom Database Table in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 offers to fetch the data from the default database table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, to&nbsp;get data from custom database table in Magento 2, one will need to implement the solution below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use this code to get data for a custom module you developed or any custom feature for which you\u2019ll need data from a custom database table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, if you want to display any custom data on the frontend, for example, show customers\u2019 mobile number on the cart page or My Account, you\u2019ll have to fetch the data for implementing the same. If you are a developer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to get data from custom database table in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>Data.php<\/strong><\/em>&nbsp;in&nbsp;<strong>app\/code\/[Vendor]\/[Module]\/Helper<\/strong><\/p>\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;\n\nuse [Vendor]\\[Module]\\Model\\CustomModelFactory;\nclass Data extends \\Magento\\Framework\\App\\Helper\\AbstractHelper\n{\n    protected $customModelFactory;\n    public function __construct(\n        Context $context,\n        CustomModelFactory $customModelFactory,\n        array $data = array()\n    )     {\n        $this->customModelFactory = $customModelFactory;\n        parent::__construct($context, $data);\n    }\n    public function getCollection()\n    {\n        return $this->customModelFactory->create()->getCollection();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, you can use a helper function to get the custom table model collection as per your requirement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Related Post<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/change-database-name-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Change Database Name in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/read-csv-file-using-root-script-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Read CSV File Using Root Script in Magento 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 offers to fetch the data from the default database table. However, to&nbsp;get data from custom database table in Magento 2, one will need&#8230;<\/p>\n","protected":false},"author":5,"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-1008","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1008","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1008"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1008\/revisions"}],"predecessor-version":[{"id":14907,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1008\/revisions\/14907"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}