Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rtechshow's avatar

Finding Duplicate Data

Hello Geeks, Thanks for helping me out every time. I stuck somewhere in code where i want to find duplicate data using laravel 5. Let i have a table users and it have an ip field for ip address which store user ip when they register so now i want to find all the users who have same ip.

I am using laravel 5.

Also If you can tell me, I want a site where one computer can have only one registered account so how can i achieve this. Thanks in advance

0 likes
18 replies
shahinul87's avatar
$users = DB::table('users')
                    ->select(array('users.ip', DB::raw('COUNT('*')'))
                    ->groupBy('users.ip')
                ->having(DB::raw('COUNT('*')'))
                    ->get();

May be something like this.

rtechshow's avatar

@toniperic bro it is showing just one result and i want all result having ip same like

7 users are there in database out of them 2 have the ip 255.255.30.12 , other 3 have ip 124.35.89.78 and the rest two have different ip respectively

So I want to grab all the users having ip address common so in this case we will get 5 users having ip 255.255.30.12 (in this case 2 users) and 124.35.89.78 (in this case 3 users) respectively and excluding the remaining two having different ip

jrdavidson's avatar

What's with this bro thing? And after reading the initial post I have a headache.

12 likes
beznez's avatar

You get a collection with get. So you just have to traverse it with a foreach loop. Here's an example:

User::where('ip', '=', $ip_address)->get();

Then in your view:

@foreach($users as $user)
    {{ $user->username }}: {{ $user->ip }}<br />
@endforeach
rtechshow's avatar

@xtremer360 @beznez @toniperic @shahinul87 Bro listen............

Forgot about everything just tell me how can i get the duplicate data from table without passing any variable just as we use in my sql this query for any particular column

SELECT ip, COUNT(ip) AS occurrences
FROM users
GROUP BY ip
HAVING occurrences > 1;

Please tell me how to convert it into laravel 5 using eloquent

1 like
constb's avatar

@rtechshow

$records = \DB::select('ip')
              ->selectRaw('count(`ip`) as `occurences`')
              ->from('users')
              ->groupBy('ip')
              ->having('occurences', '>', 1)
              ->get()
rtechshow's avatar

@constb bro getting error and by the way i also have User model

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ip' at line 1 (SQL: ip)

bashy's avatar

Bro. You must bro the SQL query before broing others into the bro. Okay bro?

19 likes
rodrigo.pedra's avatar
Level 56

If I understood, you want the Users instances with duplicate IP's, I would do a sub-query:

// app/Http/routes.php


    Route::get('duplicates', function () {
        $results = \App\Users::whereIn('id', function ( $query ) {
            $query->select('id')->from('users')->groupBy('ip')->havingRaw('count(*) > 1');
        })->get();

        return $results;
    });
11 likes
Saptarshi_Dey's avatar

$ip= AgentContact::select('id', DB::raw('COUNT() as count')) ->groupBy('ip') ->havingRaw('COUNT() > 1') ->get();

Please or to participate in this conversation.