Count duplicate columns in Laravel

Published 1 year ago by Raywachaga

Hi guys,

How do you count the duplicate columns in a database using eloquent model querying?

For example, in SQL, there's this command:

SELECT name, COUNT(email) 
FROM users
GROUP BY email 
HAVING ( COUNT(email) > 1 )

The code above will return the names from all columns with similar email addresses.

What I'm trying to achieve is to get the count of duplicate entries, i.e.

$noOfjobsFromSameCustomers = App\Workflow::count();

But I need an additional parameter to specify that the cust_id (a column in the workflows table) is duplicated, something like

$noOfjobsFromSameCustomers = App\Workflow::notDistinct()->orderBy('cust_id')->count();

You can use havingRaw:

->havingRaw('COUNT(email) > 1')

Please sign in or create an account to participate in this conversation.