2017-03-16 1 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)

또는

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

그래서 ... 스마트 무엇

이 달성의 웅변 방법은?

미리 감사드립니다. 또한 컬렉션에 filter() 방법을 사용할 수 있습니다 https://laravel.com/docs/5.4/collections#method-where

$filtered_ratings = $ratings->where('type', 'max'); 

답변

3

당신은 where 방법을 사용할 수 있습니다.

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

쿨! 나는 그 옵션을 comtemplate하지 않았다. 감사. – andcl

1

:

+0

그것은 작동합니다. 그러나'where' 옵션은 더 조밀합니다. 어쨌든 고마워. – andcl