How to Get Data From Custom Database Table in Magento 2
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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.
Any doubts about the topic or its implementation?
If so, mention them in the Comments section below. I’d be happy to help you out.
Do share the solution with Magento community via social media.
Thank you.
Related Post
Sanjay 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.
4 Comments
I am new to working in magento, I need to make a combobox that shows me data from the source table, I made the combobox inside a block in magento now how can I make the call to the corresponding table to fill it. if possible to help me, thank you
Hello,
Above solution is only for getting data from a table.
Please refer these blogs for your requirements:
https://meetanshi.com/blog/save-form-data-to-custom-table-in-magento-2/
https://meetanshi.com/blog/display-table-data-magento-2/
https://meetanshi.com/blog/create-model-file-to-perform-crud-operation-in-magento-2/
Thank You
Hi
i am tippanna pawar. Thanks for your valuable post. i have doubt above implemented helper but how to fetch data by passing particular database name.
please explain.
Hello Tippanna,
If you want to fetch the database wise data, you need to set multiple database:
app/etc/env.php // add customDB
'db' =>
array (
'table_prefix' => '',
'connection' =>
array (
'default' =>
array (
'host' => 'localhost',
'dbname' => 'magento2DB',
'username' => 'magento2DB_username',
'password' => 'magento2DB_password',
'active' => '1',
), // this array is from magento2 default
'custom' =>
array (
'host' => 'localhost', //your custom db host
'dbname' => 'customDB', //your custom db name
'username' => 'cuatomDB_username', //your custom db username
'password' => 'customDB_password', //your custom db password
'active' => '1', // 1 is for active database
), // this array is added by user
),
),
'resource' =>
array (
'default_setup' =>
array (
'connection' => 'default',
), //this is magento default array
'custom' =>
array (
'connection' => 'custom',
), //this resource is given by user
),
In Helper, create resourceConnection object
customModelFactory = $customModelFactory;
$this->connection = $resourceConnection->getConnection('custom');
parent::__construct($context, $data);
}
public function getCollection()
{
return $this->customModelFactory->create()->getCollection();
}
public function getCustomData(){
$select = $this->connection->select()->from(['table_name']); //table_name is a table in customDB, this query is to select table
$data = $this->connection->fetchAll($select);
print_r($data);
}
}
Thanks.