tomhatzer/nova-business-hours

Laravel Nova 的营业时间字段。


README

此包为 Nova 提供了一个字段,方便管理您的营业时间。

安装

composer require tomhatzer/nova-business-hours

使用方法

将以下行添加到您的 Nova 资源字段数组中

NovaBusinessHours::make('Business hours', 'business_hours'),

兼容性

使用此包与 spatie/open-hours

在您的模型中为您的营业时间字段创建一个获取器,如下所示

public function getBusinessHoursAttribute($value)
{
    $jsonDecoded = json_decode($value);

    return collect($jsonDecoded)->transform(function($day) {
        return array_filter(
            array_map(function($item) {
                if($item->isOpen) {
                    return substr_replace($item->open, ':', 2, 0) . '-' . substr_replace($item->close, ':', 2, 0);
                }

                return null;
            }, $day)
        );
    })->all();
}

在这种情况下,字段名称将是 business_hours。根据您的数据库列名自定义此名称。

之后,您可以使用它来用现有的营业时间填充 OpeningHours 类,如下所示

// Add the use at the top of each file where you want to use the OpeningHours class:
use Spatie\OpeningHours\OpeningHours;

// Get your model instance
$model = Model::find(1);

// Fill the OpeningHours class with your business hours
$openingHours = OpeningHours::create($model->business_hours);