Magento – Call to undefined method addFieldToFilter() in magento 2

collection;magento2modelresource-model

I have a model,resource model,collection file shown below.

app/code/Magge/Custom/Model/Categrory.php

namespace Magge\Custom\Model;

use Magento\Framework\Model\AbstractModel;

class Categrory extends AbstractModel
{
protected function _construct()
{
    $this->_init('Magge\Custom\Model\ResourceModel\Categrory');
    }
}

app/code/Magge/Custom/Model/ResourceModel/Categrory.php

namespace Magge\Custom\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Categrory extends AbstractDb
{
protected function _construct()
{
    $this->_init('customer_products', 'customproduct_id');
}
}

app/code/Magge/Custom/Model/ResourceModel/Categrory/Collection.php

namespace Magge\Custom\Model\ResourceModel\Categrory;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{

protected function _construct()
{
    $this->_init('Magge\Custom\Model\Categrory', 'Morello\Customproducts\Model\ResourceModel\Categrory');
}

}

I have a another module named CustomPriceApi within which I have a price.php file in Model .From this file I am try use the factory class but got the error.

namespace Insigma\CustomPriceApi\Model;

class Price implements PriceInterface {

public function __construct(
\Magge\Custom\Model\CategoryFactory $factoryCategory,
.....
)
{
$this->factoryCategory   = $factoryCategory;
}

Within its public method I use

$category = $this->factoryCategory->create()->addFieldToFilter('price_id',array('eq' => $priceId))->getData();

And I get error.I tried using addAttributetoFilter() but still get undefined method.Please help me in this regard.

Best Answer

Try below:

$category = $this->factoryCategory->create()->getCollection()->addFieldToFilter('price_id',array('eq' => $priceId))->getData();