Skip to content

Make beautiful settings page for Laravel Novaโ„ข using native fields โš™ ๐Ÿš€

Notifications You must be signed in to change notification settings

Visanduma/nova-settings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

35 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

โš™ Nova Settings

Settings management UI for Laravel Novaโ„ข dashboard using Native fields

screenshot1


๐Ÿ’  Install

composer require visanduma/nova-settings

๐Ÿ’  Configuration

Publish config & migrations

php artisan vendor:publish --tag=nova-settings

Run migration

php artisan migrate

Update config file (optional)

<?php

return [
     /* default path for load settings class */
    'settings_path' => app_path('Nova/Settings'),
];

Use trait in Model. generally in User.php

use Visanduma\NovaSettings\HasNovaSettings;

class User extends Authenticatable
{
    use HasNovaSettings;

}

Register tool in Nova Service Provider

public function tools()
{
        return [
            /* other tools */
            \Visanduma\NovaSettings\NovaSettings::make(),

        ];
}

๐Ÿ’  Create your first setting class

You can use nova-settings:make command to build a fresh settings class


php artisan nova-settings:make Contact

Adding fields is the same as in Nova Resource

class Contact extends NovaSettingsMum

{

    public $icon = 'phone';


    public function fields()

    {

        return [

            Text::make('Name')->rules('required'),

            Text::make('Address')->rules('required'),

            Text::make('City')->rules('required'),

            Select::make('Province')->options([
                'NC' => 'North Central',
                'N' => 'Northern',
            ]),

            Country::make('Country')->rules('required'),

            Panel::make('Home', [

                Text::make('Contact name'),

                Text::make('Phone'),

            ])->help('Update you home contacts'),

        ];

    }
}

๐Ÿ’  Registering settings

All settings class in the default path are automatically registered. If you are going to use a different path, please configure it in nova-settings.php

If you want to register settings class manually, use NovaSettings::register method in the service provider

namespace App\Nova\Settings;

public function boot()

{
        NovaSettings::register([
            Contact::class,
        ]);
}

๐Ÿ’  Customizing the settings class

You can customize settings class as per your needs

Customizing settings menu icon. you can use any Heroicons

public $icon = 'bell';

Customizing section label

 public function label():string

 {

    return 'User contacts';

 }

Customizing uriKey. uriKey is used when saving/retrieving the settings

 public function uriKey(): string

 {
    return 'user-contacts';
 }

๐Ÿ’  Model settings vs Global settings

There are two type of settings. Model settings & Global Settings. Model settings is always bound to an entity (Auth user by default) while global settings are not bound to any entity

You can easily configure the settings type with global property in the settings class

    protected bool $global = false;

if you want use another Model rather than User Model , just override the getModel() method on settings class

protected function getModel()
{
    return Auth::user(); // default model is Auth User
}

๐Ÿ’  Retrieving the settings

use Visanduma\NovaSettings\NovaSettings;

// getting single value
NovaSettings::get('contact.name', 'default value');

nova_settings('contact.name','default value');

// getting whole settings array
NovaSettings::get('contact');

nova_settings('contact');

// use different Model instead Auth User
NovaSettings::get('contact.name', 'default value', Admin::find(3));

nova_settings('contact.name','default value', Admin::find(3));


// getting global settings
NovaSettings::global('system.email');

NovaSettings::global('system');

nova_settings_global('system.phone'),

๐Ÿ’  Transforming inputs

If you need to customize user-submitted form data, override the following method in the settings class. This method will receive form data array as its argument

protected function transformInputs(array $inputs): array

{
    // do your modifications

    return $inputs;
}

๐Ÿ’  Hooks

protected function afterSaved(NovaRequest $request)

{
        // this method will be called after the form is saved
}

Known issues

  • File inputs does not work correctly
  • Uploaded files cannot be delete
  • Not works well with 3rd party fields

Todo

  • Authorization
  • Caching
  • Events

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Make beautiful settings page for Laravel Novaโ„ข using native fields โš™ ๐Ÿš€

Resources

Stars

Watchers

Forks