BigJobbies
BigJobbies

Reputation: 4033

Laravel return duplicate records where all data is the same

I'm wondering if its possible to get a list of all records that are exactly the same (every columns data is matched, except for the ID).

At the moment, i am returning a list of all records which are duplicates based on one column, this works good, but some of them have different data in other columns and i need to filter those ones out.

This is what I have at the moment;

$dupes = \DB::table('customers')
  ->select('phone', \DB::raw('COUNT("phone") as `count`'))
  ->groupBy('phone')
  ->having('count', '>', 1)
  ->get();

Upvotes: 0

Views: 1546

Answers (1)

Dozz
Dozz

Reputation: 46

Hello you just need to add all the columns in select and group by

\DB::table('customers')
 ->select(['phone', 'column_name', 'column_name_2'])
 ->groupBy(['phone', 'column_name', 'column_name_2'])
 ->havingRaw('COUNT(id) > 1')
 ->get()

Like this you will get duplicated rows

Upvotes: 2

Related Questions