NativePHP
Windows support is here! 🔥 Read the full announcement →
Settings

NativePHP is currently in alpha development

Let's get to beta!

#
Storing Settings

NativePHP offers an easy method to store and retrieve settings in your application. This is helpful for saving application-wide settings that persist even after closing and reopening the application.

Settings are managed using the Settings facade and are stored in a file named config.json in the appdata directory of your application.

Copied!
1use Native\Laravel\Facades\Settings;

#
Setting a value

It's as simple as calling the set method. The key must be a string.

Copied!
1Settings::set('key', 'value');

#
Getting a value

To retrieve a setting, use the get method.

Copied!
1$value = Settings::get('key');

You may also provide a default value to return if the setting does not exist.

Copied!
1$value = Settings::get('key', 'default');

If the setting does not exist, default will be returned.

#
Forgetting a value

If you want to remove a setting altogether, use the forget method.

Copied!
1Settings::forget('key');

#
Clearing all settings

To remove all settings, use the clear method.

Copied!
1Settings::clear();

This will remove all settings from the config.json file.

#
Events

#
SettingChanged

The Native\Laravel\Events\Notifications\SettingChanged event is dispatched when a setting is changed.

Example usage:

Copied!
1Event::listen(SettingChanged::class, function (SettingChanged $event) {
2 $key = $event->key; // Key of the setting that was changed
3 $value = $event->value; // New value of the setting
4});

This event can also be listened with Livewire to refresh your settings page:

Copied!
1use Livewire\Component;
2use Native\Laravel\Events\Notifications\SettingChanged;
3 
4class Settings extends Component
5{
6 protected $listeners = [
7 'native:'.SettingChanged::class => '$refresh',
8 ];
9}