# Filtering in PHP

**URL:** https://discourse.getcockpit.com/t/filtering-in-php/2849
**Category:** Uncategorized
**Created:** [August 3, 2023, 2:11pm UTC](https://discourse.getcockpit.com/t/filtering-in-php/2849 "2023-08-03T14:11:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![seank1968](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/seank1968/32/1002_2.png) [@seank1968](https://discourse.getcockpit.com/u/seank1968)
#### Post date: [August 3, 2023, 2:11pm UTC](https://discourse.getcockpit.com/t/filtering-in-php/2849/1 "2023-08-03T14:11:33Z")

</div>

I’m using the following:

```auto
    $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!

---

<div class="post-metadata">

### Author: ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)
#### Post date: [August 4, 2023, 9:50am UTC](https://discourse.getcockpit.com/t/filtering-in-php/2849/2 "2023-08-04T09:50:10Z")

</div>

Try:

````auto
$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],
]);
```
````

---

<div class="post-metadata">

### Author: ![seank1968](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/seank1968/32/1002_2.png) [@seank1968](https://discourse.getcockpit.com/u/seank1968)
#### Post date: [August 4, 2023, 10:36am UTC](https://discourse.getcockpit.com/t/filtering-in-php/2849/3 "2023-08-04T10:36:27Z")

</div>

@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:

```auto
$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 ( )’

---

<div class="post-metadata">

### Author: ![seank1968](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/seank1968/32/1002_2.png) [@seank1968](https://discourse.getcockpit.com/u/seank1968)
#### Post date: [August 4, 2023, 11:01am UTC](https://discourse.getcockpit.com/t/filtering-in-php/2849/4 "2023-08-04T11:01:42Z")

</div>

@artur Sorted it:

```auto
$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!
