Skip to content

niekbr/laravel-nova-date-range-filter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Nova Date Range Filter

Packagist Packagist Version

About

This is a configurable and ready to use filter for Laravel Nova 2 based on Nova's own date filter that displays a date range picker.

Installation

To install the filter run the following command in your Laravel Nova project:

composer require pos-lifestyle/laravel-nova-date-range-filter

Usage

Simply add this filter to the filters method in your Nova resource.

use Illuminate\Http\Request;
use PosLifestyle\DateRangeFilter\DateRangeFilter;

class CustomResource extends Resource
{
    public function filters(Request $request): array
    {
        return [
            new DateRangeFilter(),
        ];
    }
}

By default, this will create a filter named "Created at" which applies the selected date range to the created_at database column.

Customization

The filter takes up to three arguments to customize it to your needs.

Parameter Type Description Default
Name String The name of the filter how it should appear in the filter dropdown list. "Created at"
Column String The name of the database column on which the selected date range should be applied to. Illuminate\Database\Eloquent\Model::CREATED_AT
Settings Array An array of settings to customize the filter. See the configuration section below for details. []

Configuration

All available settings are provided by the included Config enum. See the full example below how to use it.

Setting Type Description Default
ALLOW_INPUT Boolean Allows the user to enter a date directly into the input field. false
DATE_FORMAT String A string of characters which are used to define how the date will be displayed in the input box. The supported characters are defined in this table. "Y-m-d"
DEFAULT_DATE Array Sets the initial selected dates.

Supply an array of date strings which follow the format Y-m-d.
null
DISABLED Boolean Entirely disables the filter. false
ENABLE_TIME Boolean Enables the time picker. false
ENABLE_SECONDS Boolean Enables seconds in the time picker. false
FIRST_DAY_OF_WEEK Integer Sets the first day of the week (0 = Sunday, 1 = Monday etc.). If a custom locale is used, this setting has no effect. 0
LOCALE String Localizes the filter. Available locales can be found here. "default"
MAX_DATE String The maximum date that a user can pick to (inclusive). null
MIN_DATE String The minimum date that a user can start picking from (inclusive). null
PLACEHOLDER String The text that is shown in the empty input box. __('Choose date range')
SHORTHAND_CURRENT_MONTH Boolean Shows the month using the shorthand version (e.g. Sep instead of September). false
SHOW_MONTHS Integer The number of months that should be showed. 1
TIME24HR Boolean Displays the time picker in 24 hour mode without AM/PM selection when enabled. false
WEEK_NUMBERS Boolean Enables the display of week numbers in the calendar. false

Full Example

use Illuminate\Http\Request;
use PosLifestyle\DateRangeFilter\DateRangeFilter;
use PosLifestyle\DateRangeFilter\Enums\Config;

class CustomResource extends Resource
{
    public function filters(Request $request): array
    {
        return [
            new DateRangeFilter('Created at', 'created_at', [
                Config::ALLOW_INPUT => false,
                Config::DATE_FORMAT => 'Y-m-d',
                Config::DEFAULT_DATE => ['2019-06-01', '2019-06-30'],
                Config::DISABLED => false,
                Config::ENABLE_TIME => false,
                Config::ENABLE_SECONDS => false,
                Config::FIRST_DAY_OF_WEEK => 0,
                Config::LOCALE => 'default',
                Config::MAX_DATE => '2019-12-31',
                Config::MIN_DATE => '2019-01-01',
                Config::PLACEHOLDER => __('Choose date range'),
                Config::SHORTHAND_CURRENT_MONTH => false,
                Config::SHOW_MONTHS => 1,
                Config::TIME24HR => false,
                Config::WEEK_NUMBERS => false,
            ]),
        ];
    }
}

Screenshots

Default configuration Showing 2 months
Date range filter (default) Date range filter (2 months)

About

A Laravel Nova date range filter.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Vue 66.5%
  • PHP 28.3%
  • SCSS 2.8%
  • JavaScript 2.4%