Filtering in PHP

I’m using the following:

    $posts = cockpit('collections')->find('Staff', [
        'filter' => 
         ['active' => true, 
        'first_name' => $name,
        ],
        'sort' => ['last_name' => 1],
    ]
    );

‘$name’ is coming from a search form and is working in this example - so if I seach for John, it pulls in all the entries with the first_name John.

How could I expand it to do following:

  1. Ignore the case?
  2. Find partial matches. For example, if I search for John it would also find Johnathon
  3. Search multiple fields - for example first_name and last_name and find matches in either? So, searching for John would return ‘John Smith’, ‘Johnathon Jones’ and ‘Sally Johnson’

Thanks!

Try:

$posts = cockpit('collections')->find('Staff', [
    'filter' => [
        'active' => true, 
        '$or' => [
            'first_name' => ['$regex' => $name, '$options' => 'i'],
            'last_name' => ['$regex' => $name, '$options' => 'i'],
        ]
    ],
    'sort' => ['last_name' => 1],
]);
```

@artur thanks - I tried that before but it doesn’t return anything!

Removing the $or section and just using the first_name or last_name line works perfectly to get entries with the search string in the name:

$posts = cockpit('collections')->find('Staff', [
    'filter' => [
        'active' => true, 

            'first_name' => ['$regex' => $name, '$options' => 'i'],

    ],
    'sort' => ['last_name' => 1],
]);

But using the $or part returns ‘Array ( )’

@artur Sorted it:

$posts = cockpit('collections')->find('Staff', [
    'filter' => [
        'active' => true, 
        '$or' => [
            ['first_name' => ['$regex' => $name, '$options' => 'i']],
            ['last_name' => ['$regex' => $name, '$options' => 'i']]
        ]
    ],
    'sort' => ['last_name' => 1],
]);

Added square brackets around the two $or options!

Thanks!