Fatal error: Uncaught Error: Call to undefined method Training\Reviews\Model\ResourceModel\Reviews\Collection::addAttributeToFilter()

magento.stackexchange https://magento.stackexchange.com/questions/303542

Question

I want SELECT COUNT (*) FROM name_table WHERE result LIKE 'likes%' to be executed, and a numeric value is displayed on the page.How do I do it? I'm a beginner. I'll be very grateful for your help!

/Block/Reviews.php

<?php

namespace Training\Reviews\Block;

use Training\Reviews\Model\ResourceModel\Reviews\Collection;


use Magento\Framework\View\Element\Template;

class Reviews extends \Magento\Framework\View\Element\Template
{




public function _prepareLayout()
{
    $this->pageConfig->getTitle()->set(__('Users Reviews'));

    return parent::_prepareLayout();
}

protected $_productCollectionFactory;

protected $_productVisibility;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Training\Reviews\Model\ResourceModel\Reviews\CollectionFactory 
$productCollectionFactory,
    array $data = []
) {
    $this->_productCollectionFactory = $productCollectionFactory;

    parent::__construct($context, $data);
}

public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();


    $collection->addAttributeToFilter('result', ['like' => 'test'])
        ->load();
    $collection->count();



    return $collection;
  }
 }

/Model/ResourceModel/Reviews.php

<?php
namespace Training\Reviews\Model\ResourceModel;
class Reviews extends 
\Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

protected function _construct()
{
$this->_init('training_reviews', 'news_id');
 }
}

Model/ResourceModel/Reviews/Collection.php

<?php
namespace Training\Reviews\Model\ResourceModel\Reviews;

class Collection extends 
\Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{

protected function _construct()
{
$this->_init(
    'Training\Reviews\Model\Reviews',
    'Training\Reviews\Model\ResourceModel\Reviews'
);
 }

} 

/Model/Reviews.php

<?php
namespace Training\Reviews\Model;
use Magento\Framework\Model\AbstractModel;
class Reviews extends AbstractModel
{

protected function _construct()
{
    $this->_init('Training\Reviews\Model\ResourceModel\Reviews');
}



} 

/view/frontend/templates/reviews.php

<?php
$data->getProductCollection();

?>
Was it helpful?

Solution

Update the code of

/Block/Reviews.php

with below and give a try.

<?php
namespace Training\Reviews\Block;
use Training\Reviews\Model\ResourceModel\Reviews\Collection;

use Magento\Framework\View\Element\Template;

class Reviews extends \Magento\Framework\View\Element\Template
{

    public function _prepareLayout()
    {
        $this->pageConfig->getTitle()->set(__('Users Reviews'));

        return parent::_prepareLayout();
    }

    protected $_productCollectionFactory;
    protected $_productVisibility;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Training\Reviews\Model\ResourceModel\Reviews\CollectionFactory 
    $productCollectionFactory,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;

        parent::__construct($context, $data);
    }

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addFieldToFilter('result', ['like' => '%test%']);
        return $collection;
    }
 }

You can add $collection->count() function if you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top