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!

1
  • 1
    I would rewrite ->select('nama_peserta', (DB::raw('COUNT(nama_peserta)'))) to ->selectRaw('nama_peserta, COUNT(nama_peserta)') Commented Mar 10, 2020 at 9:07

1 Answer 1

2

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

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

or

->havingRaw('COUNT(nama_peserta) > 1')
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you're right, my code is finally running. Thankyou so much @TsaiKoga

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.