How can I get the collection of a custom module in magento 2
You need to inject the model collection factory into your constructor
protected $mymodulemodelFactory;
public function __construct(
....
\[Namespace]\[Module]\Model\ResourceModel\[Entity]\CollectionFactory $mymodulemodelFactory,
...
)
{
...
$this->mymodulemodelFactory = $mymodulemodelFactory;
...
}
and you can use in any one of the class methods:
$collection = $this->mymodulemodelFactory->create();
You need to create three files for getting collection from custom module
1) Model, 2) ResourceModel, 3) Collection
Namespace: Prince
Module: Hello
Table: hello
1) Create Hello.php Model file at
app/code/Prince/Helloworld/Model/Hello.php
<?php
namespace Prince\Helloworld\Model;
use Magento\Framework\Model\AbstractModel;
class Hello extends AbstractModel
{
/**
* Define resource model
*/
protected function _construct()
{
$this->_init('Prince\Helloworld\Model\ResourceModel\Hello');
}
}
2) Now create Hello.php ResourceModel at
app/code/Prince/Helloworld/Model/ResourceModel/Hello.php
<?php
namespace Prince\Helloworld\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Hello extends AbstractDb
{
/**
* Define main table
*/
protected function _construct()
{
$this->_init('hello', 'id'); //hello is table of module
}
}
3) Now create Collection.php Collection file at
app/code/Prince/Helloworld/Model/ResourceModel/Hello/Collection.php
<?php
namespace Prince\Helloworld\Model\ResourceModel\Hello;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
/**
* Define model & resource model
*/
protected function _construct()
{
$this->_init(
'Prince\Helloworld\Model\Hello',
'Prince\Helloworld\Model\ResourceModel\Hello'
);
}
}
Now you can test your collection in Index.php
controller app/code/Prince/Helloworld/Controller/Index/Index.php
<?php
namespace Prince\Helloworld\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Prince\Helloworld\Model\HelloFactory;
class Index extends Action
{
protected $_modelHelloFactory;
public function __construct(
Context $context,
HelloFactory $modelHelloFactory
) {
parent::__construct($context);
$this->_modelHelloFactory = $modelHelloFactory;
}
public function execute()
{
$resultPage = $this->_modelHelloFactory->create();
$collection = $resultPage->getCollection(); //Get Collection of module data
var_dump($collection->getData());
exit;
}
}
If you want to get collection in template using block file
Another four files you need to add nad create
1. Block File
2. Layout File
3. Controller File
4. Templarte File
Create Block file Hello.php
namespace vendorName\ModuleName\Block\Index;
use Magento\Framework\App\RequestInterface;
use vendorName\ModuleName\Model\Hello;
class Hello extends \Magento\Framework\View\Element\Template
{
protected $helloModelFactory;
protected $request;
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
Hello $helloModelFactory,
RequestInterface $request,
array $data = []
) {
parent::__construct($context, $data);
$this->_helloModelFactory = $helloModelFactory;
$this->_request = $request;
}
/**
* Preparing global layout
*
* @return $this
*/
protected function _prepareLayout() {
parent::_prepareLayout();
$this->pageConfig->getTitle()->set(__('Display Collection'));
return $this;
}
public function getHello()
{
$collection = $this->_helloModelFactory->getCollection();
return $collection;
}
}
Or Create Layout file routName_controllername_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="vendorName\ModuleName\Block\Event\Index" name="newevent" template="vendorName_ModuleName::Index.phtml" />
</referenceContainer>
</body>
</page>
Create Controller File Index.php
<?php
namespace vendorName\ModuleName\Controller\Index;
use Magento\Framework\App\Action;
use Magento\Framework\View\Result\PageFactory;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @param Action\Context $context
*/
protected $resultPageFactory;
public function __construct(
Action\Context $context,
PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute() {
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}
Create Template file Index.phtml
<?php
var_dump($block->getgetHello()->getData());
I hope it's work for ever