abbasudo / laravel-purity

An elegant way to filter and sort queries in Laravel
https://abbasudo.github.io/laravel-purity/?utm_source=github&utm_medium=about
MIT License
469 stars 43 forks source link

Filter Relationship MorphTo, HasOne... #88

Open alansgonzaga opened 3 weeks ago

alansgonzaga commented 3 weeks ago

Sorry if I shouldn't open a new issue, don't know how works but I couldn't reopen #23 issue that I guess is "same problem"

I'm struggling with some relationship filters. In short, I'm having trouble filtering relationships (One to Many (Polymorphic) [morphTo]). I did a lot of debugging of the SQL query that was being executed and I saw that the morphTo relationship was never used. So I came here to look for issues, and I believe it seems to be related to #23 issue. I saw that they said that the HasOne relationship is also not recognized. Since they said that this would be fixed in the future, I did some tests here with a HasOne, but I don't think it works yet, right?

I refactored my production environment to use purity filters, and I'd like to take this opportunity to say that the package is amazing. Unfortunately, I didn't notice that this type of relationship stopped working. It used to work with my old strategy (https://github.com/savannabits/primevue-datatables).

I've tried with and without filling in the $filterFields, I've also made sure that all relationships have the return type filled in, and the Models use the Filterable trait.

I'd like to check if I'm doing something wrong or if it really is a current limitation? image image image image

alansgonzaga commented 3 weeks ago

I made myself a temporary fix image image image image

abbasudo commented 3 weeks ago

Hey @alansgonzaga. Thank you for reaching out and providing detailed information about the issue.

The issue with hasOne at #23 has been fixed. However, your issue is interesting. The main reason for that bug is at this line where we try to get the related model by getRelated() function. This function returns the same model for morphTo relations, which makes sense because morph relations have multiple models related instead of a single model. So it returns Compra in your case. It should be returning the related model for Purity to work. Without the related technicality, it should work. However, we can't validate, restrict, and rename fields (columns) of related models because we can't access them.

I'm labeling this as a bug to be fixed in future PRs.

For now, if the temporary fix you provided works, please continue using it while we look into this more. If you have any additional information or further questions, feel free to add them here. We appreciate your patience and understanding. Thank you for using the Laravel Purity package!