diff --git a/src/Filters/Resolve.php b/src/Filters/Resolve.php index 35026f5..e435b3e 100644 --- a/src/Filters/Resolve.php +++ b/src/Filters/Resolve.php @@ -37,7 +37,7 @@ class Resolve public function __construct(FilterList $filterList, Model $model) { $this->filterList = $filterList; - $this->model = $model; + $this->model = $model; } /** @@ -46,10 +46,9 @@ public function __construct(FilterList $filterList, Model $model) * @param array|string $values * * @throws Exception - * - * @return void * @throws Exception * + * @return void */ public function apply(Builder $query, string $field, array|string $values): void { @@ -66,10 +65,9 @@ public function apply(Builder $query, string $field, array|string $values): void * @param Closure $closure * * @throws Exception - * - * @return bool * @throws Exception * + * @return bool */ private function safe(Closure $closure): bool { @@ -110,10 +108,9 @@ private function validate(array|string $values = []) * @param array|string|null $filters * * @throws Exception - * - * @return void * @throws Exception * + * @return void */ private function filter(Builder $query, string $field, array|string|null $filters): void { @@ -123,17 +120,17 @@ private function filter(Builder $query, string $field, array|string|null $filter // Resolve the filter using the appropriate strategy if ($this->filterList->get($field) !== null) { //call apply method of the appropriate filter class - $this->safe(fn() => $this->applyFilterStrategy($query, $field, $filters)); + $this->safe(fn () => $this->applyFilterStrategy($query, $field, $filters)); } else { // If the field is not recognized as a filter strategy, it is treated as a relation - $this->safe(fn() => $this->applyRelationFilter($query, $field, $filters)); + $this->safe(fn () => $this->applyRelationFilter($query, $field, $filters)); } } /** * @param Builder $query - * @param string $operator - * @param array $filters + * @param string $operator + * @param array $filters * * @return void */ @@ -190,7 +187,7 @@ private function relation(Builder $query, Closure $callback) { // remove the last field until its empty $field = array_shift($this->fields); - $query->whereHas($field, fn($subQuery) => $this->applyRelations($subQuery, $callback)); + $query->whereHas($field, fn ($subQuery) => $this->applyRelations($subQuery, $callback)); } /** diff --git a/src/Traits/Filterable.php b/src/Traits/Filterable.php index 0ca4f0c..16a26ff 100644 --- a/src/Traits/Filterable.php +++ b/src/Traits/Filterable.php @@ -83,6 +83,7 @@ private function bootFilter(): void app()->bind(Resolve::class, function () { $resolver = $this->getFilterResolver(); + return new $resolver(app(FilterList::class), $this); }); } diff --git a/tests/App/Models/Post.php b/tests/App/Models/Post.php index f4bd34e..5d4530a 100644 --- a/tests/App/Models/Post.php +++ b/tests/App/Models/Post.php @@ -19,7 +19,7 @@ class Post extends Model protected $fillable = [ 'title', - 'user_id' + 'user_id', ]; public function comments(): HasMany diff --git a/tests/Feature/RelationFilterTest.php b/tests/Feature/RelationFilterTest.php index 30c4ca2..2ee34d7 100644 --- a/tests/Feature/RelationFilterTest.php +++ b/tests/Feature/RelationFilterTest.php @@ -1,6 +1,5 @@ book()->create([ 'name' => 'book', - 'description' => 'book for product' + 'description' => 'book for product', ]); $response = $this->getJson('/products?filters[book][name][$eq]=book'); @@ -95,4 +94,4 @@ public function it_can_filter_by_has_one_relation(): void $response->assertOk(); $response->assertJsonCount(1); } -} \ No newline at end of file +}