norman-huth/nova-single-resource

Laravel Nova resource and fields for single record resources

0.0.5 2022-12-08 13:56 UTC

This package is auto-updated.

Last update: 2024-11-27 23:14:48 UTC


README

Create resources for "single" resources (key-value database structure), such as a settings table.

index detail

Install

composer require norman-huth/nova-single-resource

Usage

The following description refers to the Model Settings as an example....

You can create a resource with php artisan nova:single-resource Setting.
The table still requires a primary ID and this package is designed to allow the Value column to be nullable.

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'),
        ];
    }
}

Define the sections in 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/
],

And add for every section fields.
Format: get'.Str::studly($slug).'Fields: getGeneralSettingsFields(NovaRequest $request)

Columns

By default, the columns key and value are used in the database.

If you want to use others. You must specify them in the model:

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

Change cast of a field

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

Single Resource Fields

In this resource must be used adjusted fields.

The following fields are already included:

Nova

File Fields

Package Fields (alphabetic)

Field Development Notices

  • Try this trait
  • Cast with protected string $cast. Example here. See protected function castValue in the trait

Todos

More Laravel Nova Packages

Stand With Ukraine

Woman. Life. Freedom.