skip to Main Content

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  
  }
]

3

Answers


  1. 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);
    });
    
    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
  3. 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  
        }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search