Skip to content

Commit

Permalink
Add support for starts_with operator
Browse files Browse the repository at this point in the history
The `starts_with` operator allows to search for string fields that start with a given substring
  • Loading branch information
iraklisg committed Nov 2, 2022
1 parent 18278f5 commit 0a69a83
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ protected function allowedOperators(): string
'between',
'not_between',
'contains',
'starts_with',
'between_date',
'in',
]);
Expand Down
5 changes: 5 additions & 0 deletions src/FiltersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ protected function contains(array $filter, Builder $query): Builder
return $query->where($filter['column'], 'like', '%'.$filter['query_1'].'%', $filter['match']);
}

protected function startsWith(array $filter, Builder $query): Builder
{
return $query->where($filter['column'], 'like', $filter['query_1'].'%', $filter['match']);
}

protected function betweenDate(array $filter, Builder $query): Builder
{
return $query->whereBetween($filter['column'], [
Expand Down
20 changes: 20 additions & 0 deletions tests/FiltersBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ public function it_filters_by_string_field_contains_value(): void
self::assertNotTrue($results->contains($this->client3));
}

/** @test */
public function it_filters_by_string_field_starts_with_value(): void
{
$results = Client::filter([
'filters' => [
[
'column' => 'name',
'operator' => 'starts_with',
'query_1' => 'j',
'query_2' => null,
],
],
])->get();

self::assertCount(2, $results);
self::assertTrue($results->contains($this->client1));
self::assertTrue($results->contains($this->client2));
self::assertNotTrue($results->contains($this->client3));
}

/*
|--------------------------------------------------------------------------
| Filtering date/datetime fields on model
Expand Down

0 comments on commit 0a69a83

Please sign in to comment.