Skip to content
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

Prevent query builder from overwriting scopes #257

Merged

Conversation

jonerickson
Copy link
Contributor

This PR is directly related to #256. Due to applyIncludesToQuery being after any of the filtering/scoping for a search endpoint, if your scope eager loads any of the same relationships as in the include parameter, the scope is overwritten and thus no longer applies. This PR simply reorders the query modifiers so that your explicit scope declarations will always take precedence.

Endpoint: /api/tasks/search?include=users
Scope:

public function scopeSomething(Builder $query): void
{
    $query->with(['users' => fn($query) => do something ]);
}

Scope would not be applied since the include is also eager loading the relationship and would take precedence.

@alexzarbn alexzarbn self-requested a review May 20, 2024 14:36
Copy link
Member

@alexzarbn alexzarbn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jonerickson,

Thank you for spotting this issue! Could you please include an integration test that covers that scenario as well?

@jonerickson
Copy link
Contributor Author

Tests added. If you remove the scope, or change the query modifiers in QueryBuilder.php back to the original ordering, the test will fail as the dates will result to default ordering.

@jonerickson jonerickson requested a review from alexzarbn May 20, 2024 19:18
@jonerickson
Copy link
Contributor Author

Any update @alexzarbn?

@alexzarbn alexzarbn merged commit f2b93f7 into tailflow:main Jun 2, 2024
72 checks passed
@jonerickson jonerickson deleted the feature/prevent-overwriting-scopes branch September 2, 2024 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants