Replace the ->get()->toArray() with ->toSql() and show the generated sql statement.
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();
The problem is that an user has many subjects.
perhaps use RAW query will be easier...
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?
@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.
@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
Have you checked your db? Perhaps you really got duplicates stored in.
@ep!sode I'm seeing the duplicates when do join statement if I comment it out getting exact results.
I'm seeing an inner join. How about utilizing a left join? Have you tried?
→leftJoin('subjects', 'users.id', '=', 'subjects.user_id')
@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
@ep!sode yeah it is giving more duplicates than inner join
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'...)
@if I use eager loading as you said in orderBy func it is saying unknown subjects table
Have you established the relationship between User model and Subject model?
yes.
->with(['subjects' => function ($q) {
$q->orderBy('updated_at', 'DESC');
}])
this cond is working absolutley fine
Can i see those relationships?
@ep!sode
#User Model
public function subjects()
{
return $this->hasMany(Subject::class, 'user_id');
}
How about the Subject model?
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();
@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');
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.
@snapey Thanks for your response. ah, that makes sense. what is the best practice in this case?
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.
@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.
I don't know your use-case thus I can't give you a reasonable practice in doing the proper way.
The best answer may have been much simpler if relationships were established. User-Subject models have a many-to-many rel, fyi.
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.
So, you said, you want a list of subjects. Change your perspective, get the subjects with students. Rather than the other way around.
In this article look for Ordering by has-many relationships
https://reinink.ca/articles/ordering-database-queries-by-relationship-columns-in-laravel
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
Hope this helps. I had a @foreach where I was retrieving comments, by using: unique('field.name')
@foreach ($comments->unique('commentable_id') as $comment)
<a href="/posts/{{ $comment->commentable->slug }}">
<p class="date">
{{ $comment->commentable->title }}
</p>
</a>
@endforeach
https://stackoverflow.com/questions/47189705/unique-distinct-in-laravel-foreach
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...
Please or to participate in this conversation.