Skip to content

Commit

Permalink
Date filter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
keizah7 committed Sep 17, 2020
1 parent dc972a8 commit 255dbaa
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 4 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Latest Version on Packagist](https://img.shields.io/packagist/v/keizah7/custom-date-filter.svg)](https://packagist.org/packages/keizah7/custom-date-filter)
[![Total Downloads](https://img.shields.io/packagist/dt/keizah7/custom-date-filter.svg)](https://packagist.org/packages/epartment/nova-dependency-container)
![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)
![HitCount](http://hits.dwyl.io/keizah7/nova-date-filter.svg)
[![HitCount](http://hits.dwyl.com/keizah7/nova-date-filter.svg)](http://hits.dwyl.com/keizah7/nova-date-filter)
![Forks](https://img.shields.io/github/forks/keizah7/nova-date-filter?style=social)
![Stars](https://img.shields.io/github/stars/keizah7/nova-date-filter?style=social)
![Watchers](https://img.shields.io/github/watchers/keizah7/nova-date-filter?style=social)
Expand Down Expand Up @@ -57,7 +57,14 @@ public $component = 'select-filter'; // remove this line
```
After completing these steps you can see date filter in your nova resource.
## Usage
The filter `apply` method is responsible for modifying the query to achieve the desired filter state, so you can modify it as you want
The filter `apply` method is responsible for modifying the query to achieve the desired filter state, so you can modify it as you want or use prepared package methods:
- `byTime`
- `byHour`
- `fromHour`
- `toHour`
- `byDay`
- `fromDay`
- `toDay`
```
/**
* Apply the filter to the given query.
Expand All @@ -69,10 +76,10 @@ The filter `apply` method is responsible for modifying the query to achieve the
*/
public function apply(Request $request, $query, $value)
{
return $query->where('created_at', Carbon::parse($value));
return $this->byHour($query, 'created_at', $value);
}
```
## Setup
## Settings
Default filter setings is:
```
'altFormat' => 'Y-m-d H:i'
Expand Down
109 changes: 109 additions & 0 deletions src/CustomDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Keizah7\CustomDateFilter;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Laravel\Nova\Filters\Filter;

Expand Down Expand Up @@ -65,4 +66,112 @@ public function jsonSerialize()
]
);
}

/**
* Filter resources by selected day, hour and minutes
*
* @param $query
* @param $field
* @param $value
* @return mixed
*/
public function byTime($query, $field, $value)
{
$startOfMinute = Carbon::parse($value)->startOfMinute();
$endOfMinute = $startOfMinute->copy()->endofMinute();

return $query->where($field, '>=', $startOfMinute)->where($field, '<=', $endOfMinute);
}

/**
* Filter resources by selected day and hour
*
* @param $query
* @param $field
* @param $value
* @return mixed
*/
public function byHour($query, $field, $value)
{
$startOfHour = Carbon::parse($value)->startOfHour();
$endOfHour = $startOfHour->copy()->endOfHour();

return $query->where($field, '>=', $startOfHour)->where($field, '<=', $endOfHour);
}

/**
* Filter resources from selected day and hour
*
* @param $query
* @param $field
* @param $value
* @return mixed
*/
public function fromHour($query, $field, $value)
{
$startOfHour = Carbon::parse($value)->startOfHour();

return $query->where($field, '>=', $startOfHour);
}

/**
* Filter resources to selected day and hour
*
* @param $query
* @param $field
* @param $value
* @return mixed
*/
public function toHour($query, $field, $value)
{
$startOfHour = Carbon::parse($value)->endOfHour();

return $query->where($field, '<=', $startOfHour);
}

/**
* Filter resources by selected day
*
* @param $query
* @param $field
* @param $value
* @return mixed
*/
public function byDay($query, $field, $value)
{
$startOfDay = Carbon::parse($value)->startOfDay();
$endOfDay = $startOfDay->copy()->endOfDay();

return $query->where($field, '>=', $startOfDay)->where($field, '<=', $endOfDay);
}

/**
* Filter resources from selected day
*
* @param $query
* @param $field
* @param $value
* @return mixed
*/
public function fromDay($query, $field, $value)
{
$startOfDay = Carbon::parse($value)->startOfDay();

return $query->where($field, '>=', $startOfDay);
}

/**
* Filter resources to selected day
*
* @param $query
* @param $field
* @param $value
* @return mixed
*/
public function toDay($query, $field, $value)
{
$endOfDay = Carbon::parse($value)->endOfDay();

return $query->where($field, '<=', $endOfDay);
}
}

0 comments on commit 255dbaa

Please sign in to comment.