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

deepu07's avatar
Level 11

Duplicate Records in result

Hello Mates, Here is my query somehow in results I'm getting duplicate records. I don't know where I'm doing wrong. Any help would be great. TIA

$students = User::select([
            'users.id AS user_id',
            'first_name',
            'last_name',
            'email',
            'active'
        ])
        ->leftjoin('subjects', 'subjects.user_id', '=', 'users.id')
        ->where('active', 'yes')
        ->orderByRaw('subjects.updated_at ASC')
        ->with(['subjects' => function ($q) {
            $q->orderBy('updated_at', 'DESC');
        }])
        ->get()
        ->toArray();
0 likes
32 replies
Tray2's avatar

Replace the ->get()->toArray() with ->toSql() and show the generated sql statement.

Snapey's avatar

what's the point of the join? you don't use any of the joined data

A list of students arranged by the updated date of the subject? Does not seem to make sense?

deepu07's avatar
Level 11

@snapey I wanna get the list of students orderBy Subjects.updated_at timestamp. that's why i'm doing join here. but when I use the join it is giving. the duplicates in the result.

deepu07's avatar
Level 11

@tray2 here is the general statement

select "users"."id" as "user_id", "first_name", "last_name", "email", "active" from "users" inner join "subjects" on "subjects"."user_id" = "users"."id" where "active" = ? order by "subjects"."updated_at" asc
ep!sode's avatar

Have you checked your db? Perhaps you really got duplicates stored in.

deepu07's avatar
Level 11

@ep!sode I'm seeing the duplicates when do join statement if I comment it out getting exact results.

ep!sode's avatar

I'm seeing an inner join. How about utilizing a left join? Have you tried?

→leftJoin('subjects', 'users.id', '=', 'subjects.user_id')
deepu07's avatar
Level 11

@ep!sode for inner join 74 results and for left join 99 without joins 56. this is the right results but I wanna do orderby

deepu07's avatar
Level 11

@ep!sode yeah it is giving more duplicates than inner join

ep!sode's avatar

Oh. You are using leftJoin and with() at the same time. Given that you have already established the relationship between these 2 tables. You can just simply omit the leftJoin and retain the eagerloading (→with(['subjects'...)

deepu07's avatar
Level 11

@if I use eager loading as you said in orderBy func it is saying unknown subjects table

ep!sode's avatar

Have you established the relationship between User model and Subject model?

deepu07's avatar
Level 11

yes.

->with(['subjects' => function ($q) {
            $q->orderBy('updated_at', 'DESC');
        }])

this cond is working absolutley fine

deepu07's avatar
Level 11

@ep!sode

#User Model

public function subjects()
    {
        return $this->hasMany(Subject::class, 'user_id');
    }
ep!sode's avatar

If you are still lost, you can try this:

$students = User::select([
            'users.id AS user_id',
            'first_name',
            'last_name',
            'email',
            'active', 
           'subjects.*'
        ])
        ->join('subjects', 'subjects.user_id', '=', 'users.id')
        ->where('active', 'yes')
        ->orderByRaw('subjects.updated_at ASC')
        ->get()
        ->toArray();
deepu07's avatar
Level 11

@snapey @tray2 @ep!sode I did some refactoring now it is not giving any duplicate values. but it is not doing sortBy results. I mean ->sortByDesc('subjects.updated_at') is not working at all

$students = User::with(['subjects' => function ($q){
            $q->orderBy('updated_at', 'DESC');
        }])
        ->select([
            'id',
            'first_name',
            'last_name',
            'email',
            'active'
        ])
        ->where('active', 'yes')
        ->withCount('subjects')
        ->get()
        ->sortByDesc('subjects.updated_at');
Snapey's avatar

I wanna get the list of students orderBy Subjects.updated_at timestamp. that's why i'm doing join here. but when I use the join it is giving. the duplicates in the result.

but how can you do that when you have multiple subjects per student. The order is non-deterministic because different subjects will have different dates for the same student.

deepu07's avatar
Level 11

@snapey Thanks for your response. ah, that makes sense. what is the best practice in this case?

Snapey's avatar

I don't understand why would you want to sort the users in this way? Perhaps if you can explain the requirements then we can find the right solution.

deepu07's avatar
Level 11

@snapey client wants to see a report that who did latest changes to subjects object by each student. so for this, I was using the updated_at column.

ep!sode's avatar

I don't know your use-case thus I can't give you a reasonable practice in doing the proper way.

ep!sode's avatar

The best answer may have been much simpler if relationships were established. User-Subject models have a many-to-many rel, fyi.

deepu07's avatar
Level 11

relationships are established already. If you see my last code snippet you'll get that. and here thing is orderBy/sortBy is not working at all.

Snapey's avatar

So, you said, you want a list of subjects. Change your perspective, get the subjects with students. Rather than the other way around.

warpig's avatar

Hi, what about the distinct() method? Im also trying to avoid duplicate records. This is what im trying to do via routes/web.php

'comments' => App\Models\CustomComment::distinct()->get()

Yet that does not work, I was actually trying to find out how to use that method and I saw this post. Cheers.

https://laravel.com/docs/8.x/queries#select-statements

EDIT:

Simple SQL returns non-duplicated records:

select distinct commentable_id
from comments
samross's avatar

We wanted to return a single record for each user, but instead we get multiple records for some users in the results. Looking across the result set, we can see that the "duplicate" results are not actually duplicates - the same user is being returned with multiple categories, making each row unique...

coomeet azar shagle

Please or to participate in this conversation.