Hello Everyone,
In this blog, we will learn about addAttributeToFilter Condition in Magento 2.
In Magento there are many conditions to filter collection using addAttributeToFilter or addFieldToFilter function.
addAttributeToFilter add WHERE condition in Mysql query.
Without wasting your time, let us guide you straight away. Below is a list of addAttributeToFilter Conditions in Magento 2.
LIST OF addAttributeToFilter CONDITION IN MAGENTO 2
Condition | Syntex | Example |
Equals | eq | $collection->addAttributeToFilter(‘status’, array(‘eq’ => 1)); |
Not Equals | neq | $collection->addAttributeToFilter(‘sku’, array(‘neq’ => ‘test-product’)); |
Like | like | $collection->addAttributeToFilter(‘sku’, array(‘like’ => ‘UX%’)); |
Not Like | nlike | $collection->addAttributeToFilter(‘sku’, array(‘nlike’ => ‘err-prod%’)); |
In | in | $collection->addAttributeToFilter(‘id’, array(‘in’ => array(1,3,5))); |
Not In | nin | $collection->addAttributeToFilter(‘id’, array(‘nin’ => array(1,3,5))); |
NULL | null | $collection->addAttributeToFilter(‘description’, array(‘null’ => true)); |
Not NULL | notnull | $collection->addAttributeToFilter(‘description’, array(‘notnull’ => true)); |
Greater Than | gt | $collection->addAttributeToFilter(‘id’, array(‘gt’ => 3)); |
Less Than | lt | $collection->addAttributeToFilter(‘id’, array(‘lt’ => 5)); |
Greater Than or Equals To | gteq | $collection->addAttributeToFilter(‘id’, array(‘gteq’ => 10)); |
Less Than or Equals To | lteq | $collection->addAttributeToFilter(‘id’, array(‘lteq’ => 7)); |
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is a List of Conditions which can be used with addAttributeToFilter in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.