magento How to Filter Collections

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Parameters

ParameterDetails
$addFieldToFilter($field, $condition = null){ string } The field we are adding to the filter.
$addFieldToFilter($field, $condition = null){ mixed } The definition of the filter we will use.
addAttributeToFilter($attr, $condition = null, $join = 'inner'){ string } The field we are adding to the filter.
addAttributeToFilter($attr, $condition = null, $join = 'inner'){ mixed } The definition of the filter we will use.
addAttributeToFilter($attr, $condition = null, $join = 'inner'){ ('inner','left') } The type of sql join to use when joining the EAV table.

Remarks

Filter Comparison Arguments

Magento also offers a flexible way of filtering using comparison operators as well. Here is a list of valid operators and their syntax:

All comparison arguments can be passed to the second parameter of either the addFieldToFielter() or addAttributeToFilter() methods.

$collection_of_products->addAttributeToFilter('visible',array("eq"=>1));
ComparisonArgument ArrayResulting SQL Snippet
Equalsarray("eq"=>$var)WHERE (`my_field` = $var)
Not Equalarray("neq"=>$var)WHERE (`my_field` != $var)
Likearray("like"=>$var)WHERE (`my_field` LIKE $var)
Not Likearray("nlike"=>$var)WHERE (`my_field` NOT LIKE $var)
Isarray("is"=>$var)WHERE (`my_field` IS $var)
Inarray("in"=>$var)WHERE (`my_field` IN($var))
Not Inarray("nin"=>$var)WHERE (`my_field` NOT IN($var))
Nullarray("null"=>true)WHERE (`my_field` IS NULL)
Not Nullarray("notnull"=>true)WHERE (`my_field` IS NOT NULL)
Greater Thanarray("gt"=>$var)WHERE (`my_field` > $var)
Less Thanarray("lt"=>$var)WHERE (`my_field` < $var)
Greater Than or Equalarray("gteq"=>$var)WHERE (`my_field` >= $var)
Less Than or Equalarray("lteq"=>$var)WHERE (`my_field` <= $var)
Find in Setarray("finset"=>array($var))WHERE (find_in_set($var,`my_field`)
From and Toarray("from"=>$var1, "to"=>$var2)WHERE (`my_field` >= $var1 AND `my_field` <= $var2)


Got any magento Question?