2017-05-24 78 views
2

我在Laravel集合:如何刪除集合中的重複項?

Collection {#450 ▼ 
    #items: array:2 [▼ 
    0 => Announcement {#533 ▶} 
    1 => Announcement {#553 ▶} 
    ] 
} 

它是同一個項目。如何刪除其中一個?

的完整代碼:

public function announcements() 
    { 

     $announcements = $this->categories_ann->map(function ($c) { 
      return $c->announcements->map(function ($a) { 
       $a->subsribed = true; 

       return $a; 
      }); 
     }); 

     $flattened = $announcements->groupBy("id")->flatten(); 

     return $flattened; 
    } 
+0

檢查這個https://stackoverflow.com/questions/43993581/removing-the-duplicates-from-the-object/43993750#43993750 –

回答

4
$unique = $collection->unique(); 
+0

它不爲我工作,我試過 – Blablacar

+0

如果你嘗試:$ unique = $ collection-> flatten() - > unique(); –

+0

同樣的情況 – Blablacar