norman-huth/nova-single-resource

Laravel Nova 单记录资源资源和字段

0.0.5 2022-12-08 13:56 UTC

This package is auto-updated.

Last update: 2024-08-27 22:36:28 UTC


README

为“单”资源(键值数据库结构)创建资源,例如设置表。

index detail

安装

composer require norman-huth/nova-single-resource

使用方法

以下描述以模型设置为例...

您可以使用 php artisan nova:single-resource Setting 创建资源。
表仍然需要一个主键,此包旨在允许值列可以为空。

use NormanHuth\SingleResource\Traits\ResourceTrait;

class Setting extends Resource
{
    use ResourceTrait;  // required

    public function __construct($resource = null)
    {
        $this->bootResourceTrait(); // Required
        parent::__construct($resource);
    }

    protected static function sections(): array
    {
        return [
            'general-settings' => 
            [
                'name'   => __('General Settings'),
                'icon'   => 'eye',
            ],
            'system' => 
            [
                'name'   => __('System'),
                'faIcon' => 'fa-brands fa-laravel fa-fw',
            ],
        ];
    }

    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static string $model = \App\Models\Setting::class;

    public function getGeneralSettingsFields(NovaRequest $request): array
    {
        return [
            Currency::make('Price')->currency('EUR'),
            Text::make(__('Text'), 'text'),
            Boolean::make(__('Boolean'), 'boolean'),
        ];
    }

    public function getSystemFields(NovaRequest $request): array
    {
        return [
            Date::make(__('Date'), 'Date'),
            DateTime::make(__('DateTime'), 'DateTime'),
        ];
    }
}

sections() 中定义部分

'general-settings' => // Required: unique slug
[
    'name'   => 'My Settings' // Required: Display section name
    'icon'   => 'eye', // Optional: Heroicon icon https://heroicons.com/
    'faIcon' => 'fa-brands fa-laravel fa-fw', // Optional: FontAwesome Icon (not included!) https://fontawesome.com/
],

并为每个部分添加字段。
格式: get'.Str::studly($slug).'Fields: getGeneralSettingsFields(NovaRequest $request)

默认情况下,使用数据库中的键和值作为列。

如果想要使用其他列。必须在模型中指定它们

class Setting extends Model
{
    public static string $keyColumn = 'key';
    public static string $valueColumn = 'value';

更改字段的类型转换

Text::make(__('SMTP Password'), 'smtp_password')
    ->cast('encrypted'),

单资源字段

在此资源中必须使用调整后的字段。

以下字段已经包含

Nova

文件字段

包字段(字母顺序)

字段开发通知

  • 尝试这个 特质
  • 使用 protected string $cast 进行类型转换。例如 这里。请参阅特质中的 protected function castValue

待办事项

More Laravel Nova Packages

Stand With Ukraine

Woman. Life. Freedom.