norman-huth / nova-single-resource
Laravel Nova resource and fields for single record resources
Fund package maintenance!
Ko Fi
huth.it/coffee
Installs: 2 438
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 1
Open Issues: 1
Requires
- php: ^8.0
README
Create resources for "single" resources (key-value database structure), such as a settings table.
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. Seeprotected function castValue
in the trait
Todos
- Custom
ResourceUpdateController
&Update
component to be able to use slugs in url - ebess/advanced-nova-media-library
- ???