Skip to content

Commit

Permalink
fix: filtering timestamp fields by date only
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzarbn committed Nov 18, 2021
1 parent 7fefd8f commit 6785649
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/Drivers/Standard/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ function ($relationQuery) use ($relationField, $filterDescriptor) {
);
}
} else {
$this->buildFilterQueryWhereClause($this->getQualifiedFieldName($filterDescriptor['field']), $filterDescriptor, $query, $or);
$this->buildFilterQueryWhereClause(
$this->getQualifiedFieldName($filterDescriptor['field']),
$filterDescriptor,
$query,
$or
);
}
}
}
Expand All @@ -138,8 +143,16 @@ function ($relationQuery) use ($relationField, $filterDescriptor) {
*/
protected function buildFilterQueryWhereClause(string $field, array $filterDescriptor, $query, bool $or = false)
{
if (!is_array($filterDescriptor['value'])) {
$query->{$or ? 'orWhere' : 'where'}($field, $filterDescriptor['operator'], $filterDescriptor['value']);
if ($filterDescriptor['value'] !== null &&
in_array($filterDescriptor['field'], (new $this->resourceModelClass)->getDates(), true)
) {
$constraint = 'whereDate';
} else {
$constraint = 'where';
}

if (!is_array($filterDescriptor['value']) || $constraint === 'whereDate') {
$query->{$or ? 'or' . ucfirst($constraint) : $constraint}($field, $filterDescriptor['operator'], $filterDescriptor['value']);
} else {
$query->{$or ? 'orWhereIn' : 'whereIn'}($field, $filterDescriptor['value'], 'and', $filterDescriptor['operator'] === 'not in');
}
Expand All @@ -156,8 +169,12 @@ protected function buildFilterQueryWhereClause(string $field, array $filterDescr
* @param bool $or
* @return Builder|Relation
*/
protected function buildPivotFilterQueryWhereClause(string $field, array $filterDescriptor, $query, bool $or = false)
{
protected function buildPivotFilterQueryWhereClause(
string $field,
array $filterDescriptor,
$query,
bool $or = false
) {
if (!is_array($filterDescriptor['value'])) {
$query->{$or ? 'orWherePivot' : 'wherePivot'}($field, $filterDescriptor['operator'], $filterDescriptor['value']);
} else {
Expand Down Expand Up @@ -212,11 +229,19 @@ function ($relationQuery) use ($relationField, $requestedSearchString) {
/**
* @var Builder $relationQuery
*/
return $relationQuery->where($relationField, 'like', '%'.$requestedSearchString.'%');
return $relationQuery->where(
$relationField,
'like',
'%' . $requestedSearchString . '%'
);
}
);
} else {
$whereQuery->orWhere($this->getQualifiedFieldName($searchable), 'like', '%'.$requestedSearchString.'%');
$whereQuery->orWhere(
$this->getQualifiedFieldName($searchable),
'like',
'%' . $requestedSearchString . '%'
);
}
}
}
Expand Down Expand Up @@ -257,7 +282,9 @@ public function applySortingToQuery($query, Request $request): void
}

$relationTable = $this->relationsResolver->relationTableFromRelationInstance($relationInstance);
$relationForeignKey = $this->relationsResolver->relationForeignKeyFromRelationInstance($relationInstance);
$relationForeignKey = $this->relationsResolver->relationForeignKeyFromRelationInstance(
$relationInstance
);
$relationLocalKey = $this->relationsResolver->relationLocalKeyFromRelationInstance($relationInstance);

$query->leftJoin($relationTable, $relationForeignKey, '=', $relationLocalKey)
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/StandardIndexFilteringOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,27 @@ public function getting_a_list_of_resources_filtered_by_model_field_using_nullab
$this->makePaginator([$matchingPost], 'posts/search')
);
}

/** @test */
public function getting_a_list_of_resources_filtered_by_model_date_field(): void
{
$matchingPost = factory(Post::class)->create(['publish_at' => Carbon::parse('2019-01-05 13:30:00')])->fresh();
factory(Post::class)->create(['publish_at' => Carbon::now()])->fresh();

Gate::policy(Post::class, GreenPolicy::class);

$response = $this->post(
'/api/posts/search',
[
'filters' => [
['field' => 'publish_at', 'operator' => '=', 'value' => '2019-01-05'],
],
]
);

$this->assertResourcesPaginated(
$response,
$this->makePaginator([$matchingPost], 'posts/search')
);
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Post extends Model
* @var array
*/
protected $dates = [
'publish_at',
'deleted_at' //workaround for Laravel 5.7 - SoftDeletes trait adds deleted_at column to dates automatically since Laravel 5.8
];

Expand Down

0 comments on commit 6785649

Please sign in to comment.