-
-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retrieving duplicate models in collection #2
Comments
When multiple artists in a collective are assigned to the same video, a Group the query to get unique results: public function videos()
{
return $this->hasManyDeep(Video::class, [Artist::class, 'artist_video'])
->groupBy('videos.id');
} |
Hi @staudenmeir, Thank you for clarifying the how the query works, I appreciate your help. I just updated my relationship with the code you provided where the videos are then grouped by id to give me unique results, however I am receiving the following error;
To confirm, this is how I'm currently fetching the videos to be displayed on a collectives page. $videos = $collective->videos()->latest('published_at')->simplePaginate(18); |
Sorry about that. I explicitly tested the query for this problem, but it worked. Now I get the same error. The easiest solution is probably to filter the query results: $videos = $collective->videos->unique(); I'll think about an in-query solution. |
Hi @staudenmeir, Thank you - I'll try this out shortly. However from experience I'm not sure I can paginate unique results. But I know that's not an issue directly related to this issue so will look around for a solution. Thanks for your all your help, this package is excellent. |
You're right, this isn't a solution for pagination. You can make the raw query work like this: select videos.*, min(artists.collective_id) ... But I don't see a way to override this part of the I was thinking about something like The really annoying part is that selecting |
I found a solution, check out |
Thank you so much @staudenmeir, this works perfectly. |
Is GroupBy still working? I get an error with it still.
Relationship:
|
@Skintillion You need to adjust the query: $a->e()->getQuery()->select('e.*')->get(); |
Perfect thank you. Love this package. Thanks! |
the groupBy is not working for me on the v.1.11 any thoughts? it gives me the error: Illuminate/Database/QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'product_items' (SQL: select I have this in my relationship:
If I remove the groupBy it works perfectly, just with the duplicate values Im loving this package so far just need to figure this last thing out. Thanks for any help :) |
@GDanielRG How are you querying the relationship? |
@staudenmeir BaseProduct -> has Many -> ProductOption -> has Many -> ProductVariant -> belongsToMany -> ProductItem and in my Base product I have the relationship:
And I just do
which works fine but of course because of the many to many between ProductVariant and ProductItem I get duplicate results. and when adding the |
Relationship queries with The (easier) alternative is removing duplicates from the query results: #2 (comment) |
Awesome It worked like this: added
Thank you very much, also if you have the time, any direction on how to achieve the opposite? |
The opposite relationship looks like this: class ProductItem extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function baseProducts()
{
return $this->hasManyDeep(
BaseProduct::class,
['product_item_product_variant', ProductVariant::class, ProductOption::class],
[null, null, 'id', 'id'],
[null, null, 'product_option_id', 'base_product_id']
);
}
} |
Thanks for the help! changed it to:
Since all the variants will always come from the same BaseProduct and works like wonders!!! Thanks! |
I suppose this approach won't work with eager loading? It adds "laravel_through_key" which seems to be impossible to remove from select. |
@messiahUA The You can only remove duplicates from the results: $users = User::with('permissions')->get()
->each(function (User $user) {
$user->setRelation('permissions', $user->permissions->unique());
}); |
For anyone finding this in 2021. HasManyDeep relationship declarations accept ->distinct() as an added chain at the end of the relationship declaration. No guarantees that it'll always work, but in our use case it worked perfectly to return only unique results of the distant relation. |
Is also use this, but at least for MySQL8, PHP8 and Laravel 8.x I can say that it does't not work for all cases. For example When using distinct and there are multiple relations in between that point to the same target model there would be a different result between: So if you have an influence on how the pagination works, you would need to change the query to explicitly count distinct columns. It could look something like this: However, I noticed this behavior when using Nova which creates the pagination automatically. And I have not yet figured out how to change that query. |
Hello,
I currently have the following relations;
Collective →
hasMany
→ Artist →belongsToMany
→ VideoI currently have the following
hasManyDeep
relation setup for a collective to fetch their videos.However, when I fetch the videos for a collective using
$collective->videos
I receive duplicate videos since an the same artists in a collective, can be assigned to the same video.How can I ensure that only unique videos are fetched? Is there something wrong with my relation?
The text was updated successfully, but these errors were encountered: