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

Allow the usage of more complex filter methods by Spatie\QueryBuilder #67

Conversation

bfiessinger
Copy link
Contributor

@bfiessinger bfiessinger commented Aug 9, 2024

currently sorting and filtering uses a pretty basic approach which will not always lead to the desired result.

For example you are searching a model named "Test"

{
	"filter": {
		"name": "Test"
	}
}

but this would also get the result for a model named "Testing" as the filter is fuzzy by default. But Spatie's Query Builder has multiple ways to enhance those queries. For example exact, scoped or even custom filters [link]

This PR adds support for this fine tuning.

Example:
Using an exact match filter for a model named "Test"

Model.php

<?php

use Spatie\QueryBuilder\AllowedFilter;

class Model {
    // public static array $allowedFilters = [
    //     'name'
    // ];

    public static function getAllowedFilters(): array
    {
        return [
            AllowedFilter::exact('name'),
        ];
    }
    ...
}

Congrats. Now only companies with the exact name of "Test" are shown.

Note:
The properties $allowedFields, $allowedSorts, $allowedFilters are still working.

@rupadana
Copy link
Owner

rupadana commented Aug 9, 2024

Thank you for your first contribution to this repository! 😄

maybe we have some contracts now like HasAllowedFields, HasAllowedSorts and others.

Can you into it?

@bfiessinger
Copy link
Contributor Author

@rupadana sure I can get into it. One Concern I have would be that the naming might easily collide with other packages as allowed sorts, fields, and filters might apply to other things too besides an API.

What do you think about a naming like getAllowedApiFilters and HasAllowedApiFilters, etc.

@bfiessinger
Copy link
Contributor Author

bfiessinger commented Aug 12, 2024

added Contracts in 736f6e2

I would personally suggest renaming as mentioned above and add another Contract named for example HasAllowedQueryModifiers or HasAllowedApiQueryModifiers which merges all these interfaces.

@rupadana
Copy link
Owner

added Contracts in 736f6e2

I would personally suggest renaming as mentioned above and add another Contract named for example HasAllowedQueryModifiers or HasAllowedApiQueryModifiers which merges all these interfaces.

Thank you for your suggestion, i think we still use HasAllowedFields, etc for v3. cause we will need more condition if we implemented here now.

I will bring it to v4 for better & clean code.

@rupadana rupadana merged commit 7e7dd59 into rupadana:main Aug 12, 2024
14 checks passed
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