toDateString() : $value->format('Y-m-d'); } throw new Exception("Date field must cast to 'date' in Eloquent model."); } }); } /** * The minimum value that can be assigned to the field. * * @param \Carbon\CarbonInterface|string $min * @return $this */ public function min($min) { if (is_string($min)) { $min = Carbon::parse($min); } $this->min = $min->toDateString(); return $this; } /** * The maximum value that can be assigned to the field. * * @param \Carbon\CarbonInterface|string $max * @return $this */ public function max($max) { if (is_string($max)) { $max = Carbon::parse($max); } $this->max = $max->toDateString(); return $this; } /** * The step size the field will increment and decrement by. * * @param string|int|\Carbon\CarbonInterval $step * @return $this */ public function step($step) { $this->step = $step instanceof CarbonInterval ? $step->totalDays : $step; return $this; } /** * Resolve the default value for the field. * * @return string|null */ public function resolveDefaultValue(NovaRequest $request) { /** @var \DateTimeInterface|string|null $value */ $value = parent::resolveDefaultValue($request); if ($value instanceof DateTimeInterface) { return $value instanceof CarbonInterface ? $value->toDateString() : $value->format('Y-m-d'); } return $value; } /** * Make the field filter. * * @return \Laravel\Nova\Fields\Filters\Filter */ protected function makeFilter(NovaRequest $request) { return new DateFilter($this); } /** * Define the default filterable callback. * * @return callable(\Laravel\Nova\Http\Requests\NovaRequest, \Illuminate\Database\Eloquent\Builder, mixed, string):\Illuminate\Database\Eloquent\Builder */ protected function defaultFilterableCallback() { return function (NovaRequest $request, $query, $value, $attribute) { [$min, $max] = $value; if (! is_null($min) && ! is_null($max)) { return $query->whereBetween($attribute, [$min, $max]); } elseif (! is_null($min)) { return $query->where($attribute, '>=', $min); } return $query->where($attribute, '<=', $max); }; } /** * Prepare the field for JSON serialization. * * @return array */ public function serializeForFilter() { return transform($this->jsonSerialize(), function ($field) { return Arr::only($field, [ 'uniqueKey', 'name', 'attribute', 'type', 'placeholder', 'extraAttributes', ]); }); } /** * Prepare the element for JSON serialization. * * @return array */ public function jsonSerialize(): array { return array_merge(parent::jsonSerialize(), array_filter([ 'min' => $this->min, 'max' => $this->max, 'step' => $this->step ?? 'any', ])); } }