4

Could anyone help me why the following code returns empty result when use LIKE operator with addAttributeToFilter option? Thanks!

$productcollection = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
       ->addAttributeToSelect(['name', 'price', 'image'])
       ->addAttributeToFilter('name', array('LIKE' => '%Iphone%'))
       ->setPageSize(10,1);

1 Answer 1

18

Use 'like' instead of 'LIKE'

$productcollection = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
                                                  ->addAttributeToSelect(['name', 'price', 'image'])
                                                  ->addAttributeToFilter('name', array('like' => '%Iphone%'))
                                                  ->setPageSize(10,1);

You can print query following way:

echo $productcollection->getSelect()->__toString();
2

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.