outl1ne / nova-settings
A Laravel Nova tool for editing custom settings using native Nova fields.
Fund package maintenance!
outl1ne
Installs: 444 375
Dependents: 1
Suggesters: 0
Security: 0
Stars: 277
Watchers: 7
Forks: 96
Open Issues: 14
Requires
- php: >=8.0
- laravel/nova: ^4.26
- outl1ne/nova-translations-loader: ^5.0
Requires (Dev)
- laravel/nova-dusk-suite: ^9.3-dev
- orchestra/testbench: ^7.0
- orchestra/testbench-dusk: ^7.0
- phpunit/phpunit: ^9.0
- dev-main
- 5.2.4
- 5.2.3
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.0
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- v3.x-dev
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.0
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.8
- 2.5.7
- 2.5.6
- 2.5.5
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.0
This package is auto-updated.
Last update: 2024-11-07 12:46:16 UTC
README
This Laravel Nova package allows you to create custom settings in code (using Nova's native fields) and creates a UI for the users where the settings can be edited.
Requirements
php: >=8.0
laravel/nova: ^4.26
Features
- Settings fields management in code
- UI for editing settings
- Helpers for accessing settings
- Rule validation support
Screenshots
Installation
Install the package in a Laravel Nova project via Composer and run migrations:
# Install nova-settings composer require outl1ne/nova-settings # Run migrations php artisan migrate
Register the tool with Nova in the tools()
method of the NovaServiceProvider
:
// in app/Providers/NovaServiceProvider.php public function tools() { return [ // ... new \Outl1ne\NovaSettings\NovaSettings ]; }
Usage
Registering fields
Define the fields in your NovaServiceProvider
's boot()
function by calling NovaSettings::addSettingsFields()
.
// Using an array \Outl1ne\NovaSettings\NovaSettings::addSettingsFields([ Text::make('Some setting', 'some_setting'), Number::make('A number', 'a_number'), ]); // OR // Using a callable \Outl1ne\NovaSettings\NovaSettings::addSettingsFields(function() { return [ Text::make('Some setting', 'some_setting'), Number::make('A number', 'a_number'), ]; });
Registering field panels
// Using an array \Outl1ne\NovaSettings\NovaSettings::addSettingsFields([ Panel::make('Panel Title', [ Text::make('Some setting', 'some_setting'), Number::make('A number', 'a_number'), ]), ]);
Casts
If you want the value of the setting to be formatted before it's returned, pass an array similar to Eloquent
's $casts
property as the second parameter.
\Outl1ne\NovaSettings\NovaSettings::addSettingsFields([ // ... fields ], [ 'some_boolean_value' => 'boolean', 'some_float' => 'float', 'some_collection' => 'collection', // ... ]);
Subpages
Add a settings page name as a third argument to list those settings in a custom subpage.
\Outl1ne\NovaSettings\NovaSettings::addSettingsFields([ Text::make('Some setting', 'some_setting'), Number::make('A number', 'a_number'), ], [], 'Subpage');
If you leave the custom name empty, the field(s) will be listed under "General".
To translate the page name, publish the translations and add a new key novaSettings.$subpage
to the respective translations file, where $subpage
is the name of the page (full lowercase, slugified).
Authorization
Show/hide all settings
If you want to hide the whole Settings
area from the sidebar, you can authorize the NovaSettings
tool like so:
public function tools(): array { return [ NovaSettings::make()->canSee(fn () => user()->isAdmin()), ]; }
Show/hide specific setting fields
If you want to hide only some settings, you can use ->canSee(fn () => ...)
per field. Like so:
Text::make('A text field') ->canSee(fn () => user()->isAdmin()),
Helper functions
nova_get_settings($keys = null, $defaults = [])
Call nova_get_settings()
to get all the settings formated as a regular array. Additionally, you can pass a key => value
array as a second argument: nova_get_settings(['some_key], ['some_key' => 'default_value'])
.
nova_get_setting($key, $default = null)
To get a single setting's value, call nova_get_setting('some_setting_key')
. It will return either a value or null if there's no setting with such key.
You can also pass default value as a second argument nova_get_setting('some_setting_key', 'default_value')
, which will be returned, if no setting was found with given key.
nova_set_setting_value($key, $value = null)
Sets a setting value for the given key.
Configuration
The config file can be published using the following command:
php artisan vendor:publish --provider="Outl1ne\NovaSettings\NovaSettingsServiceProvider" --tag="config"
The migration can also be published and overwritten using:
php artisan vendor:publish --provider="Outl1ne\NovaSettings\NovaSettingsServiceProvider" --tag="migrations"
Localization
The translation file(s) can be published by using the following command:
php artisan vendor:publish --provider="Outl1ne\NovaSettings\NovaSettingsServiceProvider" --tag="translations"
You can add your translations to resources/lang/vendor/nova-settings/
by creating a new translations file with the locale name (ie et.json
) and copying the JSON from the existing en.json
.
Credits
License
Nova Settings is open-sourced software licensed under the MIT license.