2017-03-16 29 views
2

我有一个雄辩的收藏就像这样(甩):搜索价值深深嵌套在一个Laravel收藏

Collection {#788 ▼ 
    #items: array:3 [▼ 
    0 => Rating {#787 ▼ 
     #table: "ratings" 
     +timestamps: false 
     #connection: null 
     #primaryKey: "id" 
     #keyType: "int" 
     #perPage: 15 
     +incrementing: true 
     #attributes: array:3 [▼ 
     "id" => 1 
     "rating" => "50" 
     "type" => "min" 
     ] 
     #original: array:3 [▶] 
     #relations: [] 
     #hidden: [] 
     #visible: [] 
     #appends: [] 
     #fillable: [] 
     #guarded: array:1 [▶] 
     #dates: [] 
     #dateFormat: null 
     #casts: [] 
     #touches: [] 
     #observables: [] 
     #with: [] 
     #morphClass: null 
     +exists: true 
     +wasRecentlyCreated: false 
    } 
    1 => Rating {#786 ▶} 
    2 => Rating {#785 ▶} 
    ] 
} 

在我的观点之一,我需要只显示与'max'型的收视率,我试图直到现在才能成功过滤收藏。 我试图至今:

if ($ratings && $ratings -> search('max') != false)

OR

if ($ratings && $ratings -> contains('max'))

所以...什么是智能和实现这一目标的雄辩方式?

在此先感谢。

回答

1

您也可以使用filter()方法的集合。

return $collection->filter(function ($value, $key) { 
    if($value->type == 'max'){ 
     return $value; 
    }  
}); 
+0

它的工作原理。但是'where'选项更紧凑。不管怎么说,还是要谢谢你。 – andcl