2

I am trying to fetch all categories with filter, but it throws following error :

1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method Magento\Catalog\Model\Category\Interceptor::addFieldToFilter

And i have also tried with addFieldToFilter and addAttributeToFilter but still it says invalid method. I want all categories filter with Display mode. Please Help!

2
  • What filter you are applying? Commented Mar 14, 2019 at 9:55
  • 'display_mode' . Commented Mar 14, 2019 at 10:00

2 Answers 2

0

Try this solution for the category filter:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\Collection');
$category->addAttributeToSelect('*')->addAttributeToFilter('parent_id',['eq'=>3]);
1
  • Please Avoid using the objectManager concept in the answers, Magento 2 strictly not encourage to use objectManager, of course, we can see some objectManager in core modules, but using objectManager directly is not a good idea. Commented Mar 14, 2019 at 10:36
1

the class Magento\Catalog\Model\Category is not a collection change your class to:

Magento\Catalog\Model\ResourceModel\Category\CollectionFactory` the solution as follows.


public function __construct(
        Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collectioFactory
        ){ $this->collectionFactory = $collectioFactory;}

public function yourFunction()
        {
            $this->collection = $this->collectionFactory->create();
            $this->collection->addAttributeToFilter('parent_id',['eq'=>3])
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.