Monday, March 13, 2017

Laravel filter nested collection

I have a question that appears to be simple.

I will quickly explain the practical problem:

  1. A radio has many coordinates.
  2. I have a collection of radios.
  3. I want to filter the coordinates based on a condition.
  4. The below code doesn't work for some reason.

    $radios = $radios->map(function ($item, $key) {
    
        $coordinates = $radio->coordinates;
        $coordinates = $coordinates->filter(function($coordinate, $key) {
    
            // return true or false;
        });
    
        $radio['coordinates'] = $coordinates;
    
        return $radio;
    });
    
    

I can filter the coordinates collection but can't "attach" the filtered array to the radio object.
What am I doing wrong?



via Jacob Nørgaard

Advertisement