Magento2 in we have collection of order, product, customer etc. For some bussiness logic we need filter collection with difeerent conditions to get data. In this blog we can see different type of condition for addAttributeToFilter and addFieldToFilter in magento2.

We can use addAttributeToFilter and addFieldToFilter we below conditions.
Here simple code of load product collection with addAttributeToFilter.

PHP
/** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory */
$collection = $this->productCollectionFactory->create();

$collection->addAttributeToFilter('sku', 'ABC');


Here mention all condition with sample code.

Equal Value (eq)

PHP
$collection->addAttributeToFilter('sku', 'ABC');
#OR
$collection->addAttributeToFilter('sku', ['eq' => 'ABC']);

# similar you can do with addFieldToFilter
$collection->addFieldToFilter('sku', 'ABC');
#OR
$collection->addFieldToFilter('sku', ['eq' => 'ABC']);

Not Equal Value (neq)

PHP
$collection->addAttributeToFilter('sku', ['neq' => 'ABC']);
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('sku', ['neq' => 'ABC']);

Like Value (like)

PHP
$collection->addAttributeToFilter('sku', ['like' => '%ABC%']);
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('sku', ['like' => '%ABC%']);

From Value to To Value (from, to)

PHP
$collection->addAttributeToFilter('size', ['from' => 35]); # INT type
$collection->addAttributeToFilter('created_at', ['from' => $date, 'date' => true]); 
$collection->addAttributeToFilter('created_at', ['to' => $date, 'date' => true]);
$collection->addAttributeToFilter('created_at', ['from' => $date, 'to' => $date];
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('size', ['from' => 35]); # INT type
$collection->addFieldToFilter('created_at', ['from' => $date, 'date' => true]); 
$collection->addFieldToFilter('created_at', ['to' => $date, 'date' => true]);
$collection->addFieldToFilter('created_at', ['from' => $date, 'to' => $date];

In Values (in)

PHP
$collection->addAttributeToFilter('entity_id', ['in' => [1,2,3]]); 
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('entity_id', ['in' => [1,2,3]]); 

Not In Values (nin)

PHP
$collection->addAttributeToFilter('entity_id', ['nin' => [1,2,3]]); 
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('entity_id', ['nin' => [1,2,3]]); 

Value Is NotNull (notnull)

PHP
$collection->addAttributeToFilter('size', ['notnull' => true]);
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('size', ['notnull' => true]);

Value Is Null (null)

PHP
$collection->addAttributeToFilter('size', ['null' => true]);
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('size', ['null' => true]);

Greater Value (gt)

PHP
$collection->addAttributeToFilter('level', ['gt' => 1]);
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('level', ['gt' => 1]);

Less Value (lt)

PHP
$collection->addAttributeToFilter('level', ['lt' => 1]);
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('level', ['lt' => 1]);

Greater Or Equal Value (gteq)

PHP
$collection->addAttributeToFilter('level', ['gteq' => 1]);
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('level', ['gteq' => 1]);

Less Or Equal Value (lteq)

PHP
$collection->addAttributeToFilter('level', ['lteq' => 1]);
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('level', ['lteq' => 1]);

Value In Set (finset)

PHP
$collection->addAttributeToFilter('website_ids', ['finset' => $websiteId]);
 
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('website_ids', ['finset' => $websiteId]);

Value Not In Set (nfinset)

PHP
$collection->addAttributeToFilter('website_ids', ['nfinset' => $websiteId]);
 
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('website_ids', ['nfinset' => $websiteId]);

Regular Expression (regexp)

PHP
$collection->addAttributeToFilter('path', ['regexp' => $regexp]);
 
# similar you can do with addFieldToFilter
$collection->addFieldToFilter('path', ['regexp' => $regexp]);

For more details you can check this class Mysql

I hope this blog is useful to use addAttributeToFilter and addFieldToFilter with different condition types in magento2. In case, I missed anything or need to add some more information, Don’t heisted to leave a comment in this blog, I’ll get back with some positive approach.

Keep loving ❤️ Keep inspiring 🤩 Keep liking 👍 No sharing 😂

1 Comment

Write A Comment