0

How can I find duplicate values in database with Laravel. I want the results like this:

name_data - amount

(Column name along with amount of duplicate data)

I used this code but it didn't work:

$duplicates = DB::table('jadwals')
    ->select('nama_peserta', (DB::raw('COUNT(nama_peserta)')))
    ->groupBy('nama_peserta')
    ->having(DB::raw('COUNT(nama_peserta)  > 1'))
    ->get();

Please help me, thank you!

Gagang
  • 41
  • 1
  • 7
  • 1
    I would rewrite ->select('nama_peserta', (DB::raw('COUNT(nama_peserta)'))) to ->selectRaw('nama_peserta, COUNT(nama_peserta)') – thephper Mar 10 '20 at 09:07

1 Answers1

2

You have a syntax error with having DB::raw(), try:

->having(DB::raw('COUNT(nama_peserta)'), '>', 1)

or

->havingRaw('COUNT(nama_peserta) > 1')
TsaiKoga
  • 12,914
  • 2
  • 19
  • 28