🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Get Data From Custom Database Table in Magento 2

By Sanjay JethvaUpdated on May 22, 2025 1 min read

Magento 2 offers to fetch the data from the default database table.

However, to get data from custom database table in Magento 2, one will need to implement the solution below.

You can use this code to get data for a custom module you developed or any custom feature for which you’ll need data from a custom database table.

Also, if you want to display any custom data on the frontend, for example, show customers’ mobile number on the cart page or My Account, you’ll have to fetch the data for implementing the same. If you are a developer.

Method to get data from custom database table in Magento 2:

Create Data.php in app/code/[Vendor]/[Module]/Helper

<?php
namespace [Vendor]\[Module]\Helper;

use [Vendor]\[Module]\Model\CustomModelFactory;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $customModelFactory;
    public function __construct(
        Context $context,
        CustomModelFactory $customModelFactory,
        array $data = array()
    )     {
        $this->customModelFactory = $customModelFactory;
        parent::__construct($context, $data);
    }
    public function getCollection()
    {
        return $this->customModelFactory->create()->getCollection();
    }
}

Now, you can use a helper function to get the custom table model collection as per your requirement.

That’s it.

Do share the solution with Magento community via social media.

Thank you.

Related Post

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.