我在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;
    }

最佳答案

$unique = $collection->unique();

关于laravel - 如何删除集合中的重复项?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44169551/

10-13 01:50