Skip to main content

Magento 2 How To Get custom table data ?

magento 2 How to get my custom table data ? To get custom table data we have to use custom module.

First of all need to created Model and Collection file associated with that tables.

As a second step, We need to add Block PHP file and inject model factory class into constructor as below.
public function __construct(
Context $context,
\Namespace\Modulename\Model\ModelNameFactory $modelNameFactory
,

array $data
= array()
) {
$this
->_modelFactory = $modelFactory;
parent
::__construct($context, $data);
}

Now 3rd step we need to Prepare a public method in your block to access collection like below code.
public function getCollectionData(){

return $this->_modelFactory->create()->getCollection();

}

Now, you need to call $this->getCollectionData() in phtml will return collection. you can use in loop.

Thank You.!

Comments

Popular posts from this blog

Magento 2 Set Title And Meta Title In Page Programatically

We can set different Meta Title and Page Heading programatically. I am giving you example. Refer below example - public function __construct ( \Magento\Framework\View\Element\Template\Context $context , \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig , \Magento\Framework\View\Page\Config $pageConfig , $data = array () ) { $this -> _scopeConfig = $scopeConfig ; $this -> _pageConfig = $pageConfig ; parent :: __construct ( $context , $data ); } /** * Prepare global layout * * @return $this */ protected function _prepareLayout () { $this -> _pageConfig -> addBodyClass ( 'advance-sitemap' ); if ( $this -> getSeoTitle ()) $this -> _pageConfig -> getTitle ()-> set ( 'Meta Title' ); if ( $this -> getMetaKeywords ()) $this -> _pageConfig -> setKeywords ( 'Meta Keywords' ); if ( $this -> getMetaDescription ()) $this -&g

Remove SID (session ID) from URL in Magento 2

Before removing SID (session ID) we should have knowledge why Magento uses SID (session ID) . Use SID on Frontend — Set to Yes if you want a user to stay logged in while switching between stores. Go to the Stores > Configuration > General > Web > Session Validation Settings > Use SID on Storefront and set its value to No   Thank You.!

Magento 2 Disable Minicart functionality Completely

The Cart Sidebar is often called the  mini cart  and displays a summary of the items in the cart. It is enabled by default and appears when you click the number of items in the Cart Link.   To Disable Mini cart Sidebar follow below steps - On the Admin sidebar, click  Stores . In the  Settings  section, choose  Configuration . In the  Sales  section on the left panel, choose  Checkout . Expand the  Shopping Cart Sidebar  section. Set  Display Shopping Cart Sidebar to No . Click  Save Config . Thank You.!