Skip to content

Commit 5515263

Browse files
authored
Fixing belongs to filtering. (#358)
* Fixing belongs to filtering. * Apply fixes from StyleCI (#359)
1 parent e2e3077 commit 5515263

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

src/Filters/SortableFilter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ public function filter(RestifyRequest $request, $query, $direction)
5353
$query->orderBy($this->column, $direction);
5454
}
5555

56-
public function usingBelongsTo(BelongsTo $field)
56+
public function usingBelongsTo(BelongsTo $field): self
5757
{
5858
$this->belongsToField = $field;
5959

60-
// $this->setColumn($field->attam ) //todo
61-
6260
return $this;
6361
}
6462

src/Sort/SortCollection.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public function __construct($items = [])
2222
? $item
2323
: SortableFilter::make();
2424

25+
if ($queryKey instanceof SortableFilter) {
26+
$unified[] = $queryKey;
27+
continue;
28+
}
29+
2530
$definition->setColumn(
2631
$definition->column ?? $queryKey
2732
);
@@ -34,7 +39,7 @@ public function __construct($items = [])
3439

3540
public function hydrateRepository(Repository $repository): self
3641
{
37-
return $this->map(fn (Filter $filter) => $filter->setRepository($repository));
42+
return $this->each(fn (Filter $filter) => $filter->setRepository($repository));
3843
}
3944

4045
public function allowed(RestifyRequest $request, Repository $repository)
@@ -73,6 +78,6 @@ public function forEager(RestifyRequest $request): self
7378

7479
public function normalize()
7580
{
76-
return $this->map(fn (SortableFilter $filter) => $filter->syncDirection());
81+
return $this->each(fn (SortableFilter $filter) => $filter->syncDirection());
7782
}
7883
}

src/Traits/InteractWithSearch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public static function collectSorts(RestifyRequest $request, Repository $reposit
9898
{
9999
return SortCollection::make(explode(',', $request->input('sort', '')))
100100
->normalize()
101-
->allowed($request, $repository)
102101
->hydrateDefinition($repository)
102+
->allowed($request, $repository)
103103
->hydrateRepository($repository);
104104
}
105105

tests/Feature/Filters/FilterDefinitionTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
use Binaryk\LaravelRestify\Filters\MatchFilter;
77
use Binaryk\LaravelRestify\Filters\SearchableFilter;
88
use Binaryk\LaravelRestify\Filters\SortableFilter;
9-
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
109
use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post;
1110
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
1211
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
1312
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
1413
use Binaryk\LaravelRestify\Tests\IntegrationTest;
15-
use Illuminate\Database\Eloquent\Builder;
1614

1715
class FilterDefinitionTest extends IntegrationTest
1816
{
@@ -72,14 +70,9 @@ public function test_can_filter_using_belongs_to_field()
7270
];
7371

7472
PostRepository::$sort = [
75-
'users.name' => SortableFilter::make()->usingBelongsTo(
73+
'users.attributes.name' => SortableFilter::make()->setColumn('users.name')->usingBelongsTo(
7674
BelongsTo::make('user', 'user', UserRepository::class),
77-
)
78-
/*function (RestifyRequest $request, Builder $builder, $direction) {
79-
$builder->join('users', 'posts.user_id', '=', 'users.id')
80-
->select('posts.*')
81-
->orderBy('users.name', $direction);
82-
}*/,
75+
),
8376
];
8477

8578
factory(Post::class)->create([
@@ -94,7 +87,9 @@ public function test_can_filter_using_belongs_to_field()
9487
]),
9588
]);
9689

97-
$json = $this->getJson(PostRepository::uriKey().'?related=user&sort=-users.name')
90+
$json = $this
91+
->withoutExceptionHandling()
92+
->getJson(PostRepository::uriKey().'?related=user&sort=-users.attributes.name')
9893
->json();
9994

10095
$this->assertSame(

0 commit comments

Comments
 (0)