I ma trying to render data from database to a custom block, but i am getting error as the below screenshot
I've review the code multiple times and still con not catch the issue
I am using Magento ver. 2.4.4
the code as the following snapshot
Model file => src/app/code/Mastering/SampleModule/Model/Item.php
<?php
namespace Mastering\SampleModel\Model;
use Magento\Framework\Model\AbstractModel;
class Item extends AbstractModel{
protected function _construct(){
$this->_init(\Mastering\SampleModule\Model\ResourceModel\Item::class);
}
}
ResourceModel file => src/app/code/Mastering/SampleModule/Model/ResourceModel/Item.php
<?php
namespace Mastering\SampleModule\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Item extends AbstractDb{
public function _construct(){
$this->_init('mastering_sample_item', 'id');
}
}
Collection file => src/app/code/Mastering/SampleModule/Model/ResourceModel/Item/Collection.php
<?php
namespace Mastering\SampleModule\Model\ResourceModel\Item;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection{
protected $_idFieldName = 'id';
public function _construct(){
$this->_init(
\Mastering\SampleModule\Model\Item::class,
\Mastering\SampleModule\Model\ResourceModel\Item::class
);
}
}
Block file => src/app/code/Mastering/SampleModule/Block/Article.php
<?php
namespace Mastering\SampleModule\Block;
use \Magento\Framework\View\Element\Template;
use Mastering\SampleModule\Model\ResourceModel\Item\Collection;
class Article extends Template
{
/**
* @var Collection
*/
private $collection;
/**
* Display constructor.
* @param Template\Context $context
* @param Collection $collection
* @param array $data
*/
public function __construct(
Template\Context $context,
Collection $collection,
array $data = []
) {
$this->collection = $collection;
parent::__construct($context, $data);
}
/**
* @return Collection
*/
public function getItems()
{
return $this->collection;
}
}
?>