-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDateRangeFilter.php
38 lines (32 loc) · 1.07 KB
/
DateRangeFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
declare(strict_types=1);
namespace Labrodev\Filters\QueryBuilder;
use Illuminate\Database\Eloquent\Builder;
use Spatie\QueryBuilder\Filters\Filter;
/**
* Class DateRangeFilter
*
* This filter allows filtering a query by a date range.
*/
class DateRangeFilter implements Filter
{
private const DEFAULT_START_DATE = '1970-01-01';
private const DEFAULT_END_DATE = '2099-12-31';
/**
* Apply the filter to the given Eloquent query.
*
* @param Builder $query The Eloquent query builder.
* @param mixed $value The value indicating whether to filter for null or not null.
* @param string $property The property or column to apply the filter to.
*
*/
public function __invoke(
Builder $query,
$value,
string $property
) {
$startDate = is_array($value) ? ($value[0] ?? self::DEFAULT_START_DATE) : self::DEFAULT_START_DATE;
$endDate = is_array($value) ? ($value[1] ?? self::DEFAULT_END_DATE) : self::DEFAULT_END_DATE;
$query->whereBetween($property, [$startDate, $endDate]);
}
}