My custom collection is not working in block file Helloworld.php
<?php
namespace New\MyModule\Block;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
class Helloworld extends Template
{
protected $_modelFactory;
public function __construct(Context $context,
New\MyModule\Model\DataFactory $modelFactory,
array $data = array())
{
$this->_modelFactory = $modelFactory;
parent::__construct($context, $data);
}
public function getHelloCollection()
{
return $this->_modelFactory->create()->getCollection();
}
}
Model files:
Model\Data.php
<?php
namespace New\Model;
use Magento\Framework\Model\AbstractModel;
class Data extends AbstractModel
{
protected function _construct()
{
$this->_init('New\MyModule\Model\ResourceModel\Data');
}
}
Resource model:
Model\ResourceModel\Data.php
<?php
namespace New\MyModel\Model\ResourceModel;
use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Data extends AbstractDb
{
protected function _construct()
{
$this->_init('blog_posts', 'post_id');
}
}
Collection file:
New\MyModule\model\ResourceModel\Data\Collection.php
<?php
namespace New\MyModule\Model\ResourceModel\Data;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init(
'New\MyModule\Model\Data',
'New\MyModule\Model\ResourceModel\Data');
}
}
I need to get the collection into my block to fetch the database values. pls help me.