Skip to content

Commit

Permalink
fix: filtering by date field in laravel 10+
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzarbn committed Mar 30, 2024
1 parent 36b4e6e commit 0c94107
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Drivers/Standard/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
Expand Down Expand Up @@ -194,8 +195,17 @@ protected function buildFilterNestedQueryWhereClause(
$query,
bool $or = false
) {
/** @var Model $resourceModel */
$resourceModel = (new $this->resourceModelClass);

$dateCasts = collect($resourceModel->getCasts())->filter(function(string $type) {
return in_array($type, ['date', 'datetime']);
})->keys()->toArray();

$dateFields = array_merge($resourceModel->getDates(), $dateCasts);

$treatAsDateField = $filterDescriptor['value'] !== null &&
in_array($filterDescriptor['field'], (new $this->resourceModelClass)->getDates(), true);
in_array($filterDescriptor['field'], $dateFields , true);

if ($treatAsDateField && Carbon::parse($filterDescriptor['value'])->toTimeString() === '00:00:00') {
$constraint = 'whereDate';
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Post extends Model
'meta' => 'array',
'options' => 'array',
'stars' => 'float',
'publish_at' => 'datetime',
];

/**
Expand Down

0 comments on commit 0c94107

Please sign in to comment.