Closed iamsubingyawali closed 1 year ago
Hi @iamsubingyawali, Good idea, I enabled it.
What database engine and version are you using?
->withPivot('activity_participant')
This is an issue when getting unique participants: Which pivot record do you want to get if a participant is in multiple activities?
Does the pivot table have more columns than activity_id
and participant_id
?
I do not have other columns in the pivot table. I only want to get participants in a program. I am using MariaDB 10.4.27.
If I don't want any values from pivot table, I think this code does the work.
return $this->hasManyDeep(Beneficiary::class, [Activity::class, 'activity_beneficiary']);
But how do I remove duplicate participants?
I think below code does the work. Is there any other way to do it?
return $this->hasManyDeep(Beneficiary::class, [Activity::class, 'activity_beneficiary'])->distinct();
I think below code does the work. Is there any other way to do it?
There are other ways, but this is the easiest.
->withPivot('activity_participant')
Do you need to get the pivot record? The issue is that you can't really have both – unique participants and the pivot record. When you only get unique participants, you "throw away" the other pivot records and so the remaining one doesn't really mean anything.
No, I don't need any pivot records. I just need to fetch the participant records based on records or relations in the pivot table.
Then ->distinct()
is the way to go.
Thanks for your help.
Hi! How about enabling discussion in this repo so that we can use it as a support forum?
I am facing an issue and I need support. Discussions would be more relevant in this case. For now, It would be really helpful if someone can help.
My table structures are:
With this schema, I want to get all the participants in a certain program. The code I tried is:
With this code I can get the participants in a certain program without any issues but if a participant is in multiple activities, it returns all those duplicate participant values as well. How do I prevent it?
For example, I have a program
P
inside which I have two activitiesA1
andA2
. I have two participantsP1
andP2
inA1
and onlyP1
inA2
. With this, I will have three total entries inactivity_participant
pivot table for the activities in that program and above code is returning all three of them. What I wanted is, only two participants entries removing the duplicateP1
participant.