Home > Software engineering >  Laravel collection filter to return data based on condition
Laravel collection filter to return data based on condition

Time:10-17

I wonder if it's possible to filter a laravel collection based on a specific attribute value.

[
  {
    "id": 2,
    "perm_id": 1,
  },
  {
    "id": 3,
    "perm_id": 1,
  },
  {
    "id": 10,
    "perm_id": 2,
  },
  {
    "id": 9,
    "perm_id": 1  
  }
]

This is how the structure of my collection looks like. I have multiple objects with the same perm_id. I need to return the object with the highest id for each perm_id. So the final return value should be like:

[
  {
    "id": 10,
    "perm_id": 2,
  },
  {
    "id": 9,
    "perm_id": 1  
  }
]

CodePudding user response:

You can group all items by perm_id and then map them to get the highest id.

$arr = [
    (object) [
        "id" => 2,
        "perm_id" => 1,
    ],
    (object) [
        "id" => 3,
        "perm_id" => 1,
    ],
    (object) [
        "id" => 10,
        "perm_id" => 2,
    ],
    (object) [
        "id" => 9,
        "perm_id" => 1  
    ]
];

$filtered = collect($arr)->sortByDesc('id')->groupBy('perm_id')->map(fn($item) => $item[0]);

dd($filtered);

CodePudding user response:

Try this->

$arr = [
    [
        "id" => 2,
        "perm_id" => 1,
    ],
    [
        "id" => 3,
        "perm_id" => 1,
    ],
    [
        "id" => 10,
        "perm_id" => 2,
    ],
    [
        "id" => 9,
        "perm_id" => 1
    ]
];

$filtered = collect($arr)   // convert array to Collection
    ->sortByDesc('id')      // sort by id, in descending order
    ->unique('perm_id')     // get perm_id's 1st occurrence value as it is sorted, it will get highest value
    ->values()              // reset array keys
    ->toArray()             // convert back to array
;

Output:

[
    {
        "id": 10,
        "perm_id": 2,
    },
    {
        "id": 9,
        "perm_id": 1  
    }
]

CodePudding user response:

This is find in the documentation of Laravel: Using a map function you can filter by an atribute value. https://laravel.com/docs/9.x/collections

$collection = collect(['taylor', 'abigail', null])->map(function ($name) {
return strtoupper($name);
})->reject(function ($name) {
    return empty($name);
});
  • Related