How to filter a database collection with the addFieldToFilter() method?

Context: How to filter a database collection?

A base abstract method

Specification

Add field filter to collection

If $condition integer or string - exact value will be filtered (‘eq’ condition)

If $condition is array - one of the following structures is expected:

["from" => $fromValue, "to" => $toValue]
["eq" => $equalValue]
["neq" => $notEqualValue]
["like" => $likeValue]
["in" => [$inValues]]
["nin" => [$notInValues]]
["notnull" => $valueIsNotNull]
["null" => $valueIsNull]
["moreq" => $moreOrEqualValue]
["gt" => $greaterValue]
["lt" => $lessValue]
["gteq" => $greaterOrEqualValue]
["lteq" => $lessOrEqualValue]
["finset" => $valueInSet]

If non matched - sequential parallel arrays are expected and OR conditions
will be built using above mentioned structure.

Example:

$field = ['age', 'name'];
$condition = [42, ['like' => 'Mage']];

The above would find where age equal to 42 OR name like %Mage%.

\Magento\Framework\Data\Collection\AbstractDb::addFieldToFilter()

Usage examples

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

1 Like

See also: