How to remove duplicates in collection?

前端 未结 3 1675
逝去的感伤
逝去的感伤 2021-02-12 11:56

I have collection in Laravel:

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

3条回答
  •  情书的邮戳
    2021-02-12 12:26

    You can unique the data set from the SQL level with groupBy + Max Try below

    YourModelName::select(DB::raw('max(`town_short_name`) as town' 'town_long_name'))
            ->groupBy(['town_short_name'])
            ->without('postalCode','country','countryState')
            ->where('town_short_name','like','%'.$townName.'%')
            ->limit(100)
            ->orderBy('town_short_name');
    

提交回复
热议问题