Magento 2 Product Collection: addAttributeToFilter with LIKE Operator Not Working

collection;magento2product-collection

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);

Best Answer

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();