2017-03-13 142 views
0

我有一个看起来很简单的问题。Laravel过滤器嵌套集合

我会很快解释实际问题:

  1. 一个无线电有很多坐标
  2. 我收集了收音机
  3. 我想过滤基于条件的坐标
  4. 下面的代码由于某种原因不起作用。

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

我可以过滤收集坐标,但不能“附加”经滤波的阵列到无线电对象。
我在做什么错?

回答

0

编辑:

试图改变自己的功能,这一点:

$radios = $radios->map(function ($radio, $key) { 

    $coordinates = $radio->coordinates; 
    $coordinates = $coordinates->filter(function($coordinate, $key) { 

     // return true or false; 
    }); 

     $radio[$key] = $coordinates; 

     return $radio; 
    }); 
+0

我不知道我跟随。请你详细说明一下吗? –

+0

无线电返回什么? – Onix

+0

它应该返回一个带有坐标集合的收音机,其中一些坐标被过滤掉。但是当我返回Response :: json($ radios-> values() - > all());我得到未过滤的坐标集合。 –