Skip to content

Conversation

ragulka
Copy link

@ragulka ragulka commented Apr 25, 2025

Based on #313 (comment), this PR adds a config option to skip checking non-migrated default values when saving settings.

Reasoning

The idea is to allow using saving settings even if migrations take a while to complete, or when one does not want to use settings migrations at all.

The latter may sound odd at first, but when Settings classes declare default values, it would allow adding new settings without creating migrations. The new settings with their default values would be "migrated" just-in-time, when the user first saves the settings. This is especially helpful in multi-tenancy apps, where each tenant (user) has their own set of settings.

Backgrounnd

I don't really see a reason to throw an exception when trying to save settings with default values, if there's nothing in the database 🤔 ...
Perhaps I am missing something, but this PR does not change the default behaviour, it only provides a lever for those of us who like to live on the edge 😂

Naming and docs

Please note that there's no docs yet, as I wanted to gauge if a PR like this would even be considered, first. If the general approach is acceptable, we can discuss naming and implementation details, and then write the docs.

Misc

At first, I thought of using a generic throw_on_missing_settings config value, but realized that would mean if a settings class does not have default values defined, then accessing such a property, or converting the class to an array would still throw a runtime error, so it wouldn't really buy anything.

@ragulka ragulka changed the title Allow configuring whether to check missing default values when saving settings Make missing default values check on save configurable Apr 25, 2025
@lloricode
Copy link
Contributor

got here, since we need user level settings

@@ -141,7 +141,10 @@ private function ensureNoMissingSettings(
->getReflectedProperties()
->keys()
->diff($properties->keys())
->when($operation === 'saving', fn (Collection $collection) => $collection->concat($config->getDefaultValueLoadedProperties()))
->when(
$operation === 'saving' && config('settings.check_missing_default_values_when_saving_settings'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, please add default value true here, so would not break on existing app

Copy link
Contributor

@lloricode lloricode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add default value when getting new config on vendor level

@triasrahman
Copy link

Is there any plan to merge this PR? @lloricode

@Tiamenti
Copy link

Tiamenti commented Sep 2, 2025

I use this package together with filament/spatie-laravel-settings-plugin. In my case, most settings were always null by default. Before v3.4.3, everything worked and did not require extra time spent on creating and managing migrations.

Making throwing an exception optional would be very useful. Perhaps without it, many, like me, have to stay on v3.4.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants