在Laravel中过滤集合

时间:2016-11-27 22:47:11

标签: php laravel laravel-5.2 laravel-5.3

我有一套Laravel设计系列:

"xdo45ttnqhsb" => array:5 [▼
      "design_id" => "xdo45ttnqhsb"
      "design_name" => "hufflepuff house"
      "category" => "harry potter"
      "tags" => array:6 [▶]
      "article_owner" => "1728"
    ]
    "wpy8r2erkk2t" => array:5 [▼
      "design_id" => "wpy8r2erkk2t"
      "design_name" => "ravenclaw house"
      "category" => "harry potter"
      "tags" => array:6 [▶]
      "article_owner" => "1728"
    ]
    "8oy7sb7i98q0" => array:5 [▼
      "design_id" => "8oy7sb7i98q0"
      "design_name" => "be positive friend"
      "category" => "miscelanea"
      "tags" => array:5 [▶]
      "article_owner" => "1728"
    ]
    "a9m79qc6bl9x" => array:5 [▼
      "design_id" => "a9m79qc6bl9x"
      "design_name" => "final fantasy pixel"
      "category" => "pixelart"
      "tags" => array:5 [▶]
      "article_owner" => "1728"
    ]

我想采用属于单一类别的设计。

例如:我只需要使用值为“harry potter”的“类别”设计。

我可以使用哪种收集助手?

3 个答案:

答案 0 :(得分:0)

为此,您可以使用适用于您的->filter()方法。

$collection->filter(function($arr){
    return $arr['category'] = "harry potter";
});

答案 1 :(得分:0)

laravel collections开始,根据我的知识,您可以使用filter()过滤记录,使用filter()进行编码,如下所示:

$collection->filter(function ($arrValue, $key) {
   return (isset($arrValue['category']) && $arrValue['category'] == "harry potter");
});

希望这有助于您解决问题。

答案 2 :(得分:0)

您可以像其他人建议的那样使用filter()或使用:

$collection->where('category', '=', 'harry potter')->get()

这将返回上一个集合中category等于harry potter的项目集合。