A Laravel Nova tool for editing custom settings using native Nova fields.
bmellink/nova-settings is a Laravel package for a laravel nova tool for editing custom settings using native nova fields..
It currently has 0 GitHub stars and 103 downloads on Packagist (latest version v1.0).
Install it with composer require bmellink/nova-settings.
Discover more Laravel packages by bmellink
or browse all Laravel packages to compare alternatives.
Last updated
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.
This version also runs on php 7.x.
php: >=7.4laravel/nova: ^4.0
Install the package in a Laravel Nova project via Composer and run migrations:
# Install nova-settings
composer require bmellink/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 \bmellink\NovaSettings\NovaSettings
];
}
Define the fields in your NovaServiceProvider's boot() function by calling NovaSettings::addSettingsFields().
// Using an array
\bmellink\NovaSettings\NovaSettings::addSettingsFields([
Text::make('Some setting', 'some_setting'),
Number::make('A number', 'a_number'),
]);
// OR
// Using a callable
\bmellink\NovaSettings\NovaSettings::addSettingsFields(function() {
return [
Text::make('Some setting', 'some_setting'),
Number::make('A number', 'a_number'),
];
});
// Using an array
\bmellink\NovaSettings\NovaSettings::addSettingsFields([
Panel::make('Panel Title', [
Text::make('Some setting', 'some_setting'),
Number::make('A number', 'a_number'),
]),
]);
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.
\bmellink\NovaSettings\NovaSettings::addSettingsFields([
// ... fields
], [
'some_boolean_value' => 'boolean',
'some_float' => 'float',
'some_collection' => 'collection',
// ...
]);
Add a settings page name as a third argument to list those settings in a custom subpage.
\bmellink\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).
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()),
];
}
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()),
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']).
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.
Sets a setting value for the given key.
The config file can be published using the following command:
php artisan vendor:publish --provider="bmellink\NovaSettings\NovaSettingsServiceProvider" --tag="config"
| Name | Type | Default | Description |
| --------------------- | ------- | ----------------- | ---------------------------------------------------------------------------------- |
| base_path | String | nova-settings | URL path of settings page. |
| reload_page_on_save | Boolean | false | Reload the entire page on save. Useful when updating any Nova UI related settings. |
| models.settings | Model | Settings::class | Optionally override the Settings model. |
The migration can also be published and overwritten using:
php artisan vendor:publish --provider="bmellink\NovaSettings\NovaSettingsServiceProvider" --tag="migrations"
The translation file(s) can be published by using the following command:
php artisan vendor:publish --provider="bmellink\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.
Nova Settings is open-sourced software licensed under the MIT license.