2016-11-22 55 views
3

是否有任何方法可以从多维集合中移除属性?Laravel Collections - 除多维集合外使用

e.g我

public function getPossibleAnswersAttribute() 
{ 
    return collect([ 
     [ 
      'option' => 'A', 
      'answer' => $this->answer_1, 
      'points' => $this->answer_1_value 
     ], 
     [ 
      'option' => 'B', 
      'answer' => $this->answer_2, 
      'points' => $this->answer_2_value 
     ], 
     [ 
      'option' => 'C', 
      'answer' => $this->answer_3, 
      'points' => $this->answer_3_value 
     ], 
     [ 
      'option' => 'D', 
      'answer' => $this->answer_4, 
      'points' => $this->answer_4_value 
     ] 
    ]); 
} 

public function getPossibleAnswersWithoutPointsAttribute() 
{ 
    $answers = $this->getPossibleAnswersAttribute() 
    ->except(['0.points']); 
    return $answers; 
} 

我试图得到同样的集合,但没有分键/属性。

我知道我能做到这一点喜欢的东西

->map(function ($item) { 
    unset($item['points']); 
    return $item; 
}); 

但是我希望有从第一删除它这样做的更流畅的方式,因为我发现我可以做->except(['0.points']);,我在想有没有像魔术关键词那样被视为关键?类似于->except(['#.points']);所以它为每个?

回答

0

现在(Laravel 5.3)的确没有比这个map(或transform)方法更简单的方法。当你稍后再回到代码中时,没有什么比较容易理解的。

虽然你可以扩展Collection,并在except中实现通配符,类似于pluck这样做,但对我来说这是一个矫枉过正。