diff --git a/README.md b/README.md index 743dab4..fc0150c 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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. @@ -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' diff --git a/src/CustomDateFilter.php b/src/CustomDateFilter.php index e821d57..516fa3c 100644 --- a/src/CustomDateFilter.php +++ b/src/CustomDateFilter.php @@ -2,6 +2,7 @@ namespace Keizah7\CustomDateFilter; +use Carbon\Carbon; use Illuminate\Http\Request; use Laravel\Nova\Filters\Filter; @@ -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); + } }