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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,28 @@ Token Resource is protected by TokenPolicy. You can disable it by publishing the
### Filtering & Allowed Field

We used `"spatie/laravel-query-builder": "^5.3"` to handle query selecting, sorting and filtering. Check out [the spatie/laravel-query-builder documentation](https://spatie.be/docs/laravel-query-builder/v5/introduction) for more information.
You can specified `allowedFilters` and `allowedFields` in your model. For example:

In order to allow modifying the query for your model you can implement the `HasAllowedFields`, `HasAllowedSorts` and `HasAllowedFilters` Contracts in your model.

```php
class User extends Model {
class User extends Model implements HasAllowedFields, HasAllowedSorts, HasAllowedFilters {
// Which fields can be selected from the database through the query string
public static array $allowedFields = [
'name'
];
public function getAllowedFields(): array
{
// Your implementation here
}

// Which fields can be used to sort the results through the query string
public static array $allowedSorts = [
'name',
'created_at'
];
public function getAllowedSorts(): array
{
// Your implementation here
}

// Which fields can be used to filter the results through the query string
public static array $allowedFilters = [
'name'
];
public function getAllowedFilters(): array
{
// Your implementation here
}
}
```

Expand Down
8 changes: 8 additions & 0 deletions src/Contracts/HasAllowedFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rupadana\ApiService\Contracts;

interface HasAllowedFields
{
public static function getAllowedFields(): array;
}
8 changes: 8 additions & 0 deletions src/Contracts/HasAllowedFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rupadana\ApiService\Contracts;

interface HasAllowedFilters
{
public static function getAllowedFilters(): array;
}
8 changes: 8 additions & 0 deletions src/Contracts/HasAllowedSorts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rupadana\ApiService\Contracts;

interface HasAllowedSorts
{
public static function getAllowedSorts(): array;
}
8 changes: 4 additions & 4 deletions stubs/PaginationHandler.stub
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class PaginationHandler extends Handlers {
$model = static::getModel();

$query = QueryBuilder::for($query)
->allowedFields($model::$allowedFields ?? [])
->allowedSorts($model::$allowedSorts ?? [])
->allowedFilters($model::$allowedFilters ?? [])
->allowedIncludes($model::$allowedIncludes ?? null)
->allowedFields($model::getAllowedFields() ?? [])
->allowedSorts($model::getAllowedSorts() ?? [])
->allowedFilters($model::getAllowedFilters() ?? [])
->allowedIncludes($model::getAllowedIncludes() ?? [])
->paginate(request()->query('per_page'))
->appends(request()->query());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,28 @@ public function handler()
{
$model = static::getModel();

$allowedFields = method_exists($model, 'getAllowedFields') && is_array($model::getAllowedFields())
? $model::getAllowedFields()
: (property_exists($model, 'allowedFields') && is_array($model::$allowedFields)
? $model::$allowedFields
: []);

$allowedSorts = method_exists($model, 'getAllowedSorts') && is_array($model::getAllowedSorts())
? $model::getAllowedSorts()
: (property_exists($model, 'allowedSorts') && is_array($model::$allowedSorts)
? $model::$allowedSorts
: []);

$allowedFilters = method_exists($model, 'getAllowedFilters') && is_array($model::getAllowedFilters())
? $model::getAllowedFilters()
: (property_exists($model, 'allowedFilters') && is_array($model::$allowedFilters)
? $model::$allowedFilters
: []);

$query = QueryBuilder::for($model)
->allowedFields($model::$allowedFields ?? [])
->allowedSorts($model::$allowedSorts ?? [])
->allowedFilters($model::$allowedFilters ?? [])
->allowedFields($allowedFields)
->allowedSorts($allowedSorts)
->allowedFilters($allowedFilters)
->paginate(request()->query('per_page'))
->appends(request()->query());

Expand Down