Skip to content

fix(Matches): Correctly match Booleans: GET converts them to 0 and 1 #632

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

Merged
merged 2 commits into from
Jun 9, 2025
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
2 changes: 1 addition & 1 deletion src/Filters/MatchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function filter(RestifyRequest $request, Builder|Relation $query, $value)
break;
case RestifySearchable::MATCH_BOOL:
case 'boolean':
if ($value === 'false') {
if ($value === 'false' || $value === '0') {
$query->where(function ($query) use ($field) {
if ($this->negation) {
return $query->where($field, true);
Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/Filters/MatchFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Binaryk\LaravelRestify\Contracts\RestifySearchable;
use Binaryk\LaravelRestify\Filters\MatchFilter;
use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post;
use Binaryk\LaravelRestify\Tests\Fixtures\Post\PostRepository;
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
Expand Down Expand Up @@ -175,6 +176,26 @@ public function test_can_match_date(): void
$this->getJson(UserRepository::route(query: ['created_at' => '2020-12-01']))->assertJsonCount(3, 'data');
}

public function test_can_match_boolean(): void
{
PostRepository::$match = [
'is_active' => 'boolean',
];

Post::factory(2)->sequence(
['is_active' => true, 'title' => 'Active post 1'],
['is_active' => false, 'title' => 'Inactive post 2'],
)->create();

$this->getJson(PostRepository::route())->assertJsonCount(2, 'data');

$this->getJson(PostRepository::route(query: ['is_active' => true]))
->assertJsonCount(1, 'data');

$this->getJson(PostRepository::route(query: ['is_active' => false]))
->assertJsonCount(1, 'data');
}

public function test_can_match_datetime_interval(): void
{
User::factory()->state([
Expand Down
3 changes: 3 additions & 0 deletions tests/Fixtures/Post/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PostRepository extends Repository

public static array $match = [
'title' => RestifySearchable::MATCH_TEXT,
'is_active' => RestifySearchable::MATCH_BOOL,
];

public static array $middleware = [];
Expand All @@ -50,6 +51,8 @@ public function fields(RestifyRequest $request): array
field('description')->storingRules('required')->messages([
'required' => 'Description field is required',
]),

field('is_active'),
];
}

Expand Down