DateRange

# Make

The DateRange field includes all the basic methods and allows you to select a date range.

Since the date range has two values, you need to specify them using the fromTo() method.

fromTo(string $fromField, string $toField)
use MoonShine\Fields\DateRange;
 
//...
 
public function fields(): array
{
return [
DateRange::make('Dates')
->fromTo('date_from', 'date_to')
];
}
 
//...

# Date and time

Using the withTime() method allows you to enter date and time into fields.

withTime()
use MoonShine\Fields\DateRange;
 
//...
 
public function fields(): array
{
return [
DateRange::make('Dates')
->fromTo('date_from', 'date_to')
->withTime()
];
}
 
//...

# Format

The format() method allows you to change the display format of field values in preview.

format(string $format)
use MoonShine\Fields\DateRange;
 
//...
 
public function fields(): array
{
return [
DateRange::make('Dates')
->fromTo('date_from', 'date_to')
->format('d.m.Y')
];
}
 
//...

# Attributes

Does the DateRange field have attributes? which can be set through the appropriate methods.

Methods min() and max() are used to set the minimum and maximum date values.

min(string $min)
max(string $max)

The step() method is used to set the date step for a field.

step(int|float|string $step)
use MoonShine\Fields\DateRange;
 
//...
public function fields(): array
{
return [
DateRange::make('Dates')
->fromTo('date_from', 'date_to')
->min('2023-01-01')
->max('2023-12-31')
->step(5)
];
}
 
//...

If you need to add custom attributes for fields, you can use the appropriate methods fromAttributes() and toAttributes().

fromAttributes(array $attributes)
toAttributes(array $attributes)
use MoonShine\Fields\DateRange;
 
//...
 
public function fields(): array
{
return [
DateRange::make('Dates')
->fromTo('date_from', 'date_to')
->fromAttributes(['placeholder'=> 'from'])
->toAttributes(['placeholder'=> 'to'])
];
}
 
//...

# Filter

While using the DateRange field to construct a filter, method fromTo() is not used, because filtering occurs on one field in the database table.

use MoonShine\Fields\DateRange;
 
//...
 
public function filters(): array
{
return [
DateRange::make('Dates', 'dates')
];
}
 
//...