dimanche 11 février 2018

laravel collection: keep ONLY duplicates

Using Laravel5's collections, I understand how to retrieve all unique items. What I would like to do is find a way to keep ONLY items in a collection that ARE duplicates. Its proving to be more difficult than I thought it would be. My use case: 1.) I query a table and retrieve a field, preserving its id as key:

$poolResults = $pool->where($key, '!=', $nullCheck)
                    ->where('dupCheck', '=', 0)->pluck('ShipmentNumber', 'id');

I then update the field I care about and remove all non numeric char so I can find dups in this field:

  $results = $poolResults->map(function ($item) use ($pattern) {
                $item = rtrim(preg_replace($pattern, '', $item),0);
                return $item;
            });

I've tried a number of approaches, such as turning this collection to an array and counting the occurrence of the values, keeping any > 1:

array_count_values($results->toArray()))

But the issue with that is that I lose my key of ID. I've tried to take the above mentioned array and somehow INTERSECT my original array.

Nothing much is working. I would appreciate some suggestions, I've probably overlooked a much easier method.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire