Search code examples
phplaravellaravel-5laravel-collection

Laravel: Filter collection on property, with a twist


I am looking for an elegant (and comprehensible in a year's time) solution for a collection filtering challenge. I am querying a log database and get some sort of the following data structure

| refId   | isDeleted   | loggedDate          |
|---------|-------------|---------------------|
| 1       | 0           | 2018-02-23 21:39:05 |
| 2       | 0           | 2018-02-23 21:39:05 |
| 3       | 0           | 2018-02-23 21:39:05 |
| 4       | 0           | 2018-02-23 21:39:05 |
| 2       | 1           | 2018-02-23 21:49:05 |
| 5       | 0           | 2018-02-23 21:49:05 |
| 6       | 1           | 2018-02-23 21:49:05 |

My objective is to filter out the entries which have both, isDeleted=0 && isDeleted=1, and to leave only those which are either isDeleted=0 or isDeleted=1. In this example I should end up with datasets of 1, 3, 4, 5 and 6.

Thx.

PS. The collection might hold up to 1000 entries, if you were to ask...


Solution

  • Maybe you could group by refId and isDeleted and filter the groups that only have one entry.

    $refIds = $collection->groupBy(['refId', 'isDeleted'])->filter(function($refGroup) {
        return count($refGroup) === 1;
    });
    

    I can't currently test this to verify it works, I just tried to come up with an approximation for how I would do this in SQL based on the Laravel docs.

    The result would still be grouped though, so it might not be that handy depending on how you're using it.