Download the PHP package metalinked/laravel-settings-kit without Composer

On this page you can find all versions of the php package metalinked/laravel-settings-kit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-settings-kit

Laravel Settings Kit

Tests GitHub Release Total Downloads License

Typed settings for Laravel, defined in code, with global values and per-user (or per-model) overrides.

Contents

Why this package

When to use it

The package covers three kinds of values, all declared the same way:

Kind Examples How
Application settings Site name, maintenance mode, global limits, API keys Global values: Settings::get() / Settings::set()
Personal preferences Theme, language, items per page, notifications overridable settings, read and written on the user
Team or tenant settings Plan limits, branding, a team's default locale overridable settings scoped to another model (overridable(Team::class))

A model inherits the global value until it stores its own, so the same key can be an app-wide default and a personal choice at once.

It is not the right tool when:

Installation

Requires PHP 8.2+ and Laravel 11, 12 or 13.

Publish the config file to declare your settings:

Defining settings

In the config file

Option Meaning
type string, boolean, integer, float, array, select or enum. Inferred from default, options or enum when omitted.
default Value used while nothing is stored.
options Allowed values of a select, as a list or as value => label.
enum Backed enum class of an enum.
overridable true, or a list of model classes that may store their own value.
rules Extra Laravel validation rules, applied after the type check.
nullable Whether null is a valid value.
encrypted Store the value encrypted. See Encrypted settings.
config Laravel config key that applyConfig() writes the stored value to. See Laravel configuration.
group Group of the setting in panels and commands.
label, description Texts or translation keys for settings screens.
meta Any extra data for your UI, returned by panel().

Unknown options throw an exception, so typos are caught early.

With the fluent builder

Useful for enums and for packages or modules that register their own settings. Call it from a service provider's boot():

Keys and groups

Keys can use dots to organise them. The first segment is the default group: appearance.theme belongs to appearance, and keys without a dot belong to general. Use group to choose another.

Reading and writing

In Blade:

Stored values and defaults

A setting uses the default from its definition until a value is saved. Changing the default in code reaches every installation that has not saved one.

Saving stores the value even when it equals the default. From then on has() returns true, and a later change of the default does not affect it. This also applies to a form that submits every field. To make a setting follow the default again, call forget().

Per-user (or per-model) settings

Add the trait to any model:

Without the trait, or if your model already has a settings attribute, use Settings::for($model). You can also pass a morph type and id, which avoids loading the model:

Only settings marked overridable can be overridden. Calling set() with any other key on a model throws SettingNotOverridableException. Overrides are deleted when the model is deleted; soft-deleted models keep them until they are force-deleted.

Changing a global value immediately affects every model that has not overridden it.

Forms

fill() saves a submitted form in one call:

The array maps setting keys to values. In HTML, name each input after its key, as in name="settings[mail.port]". Because of the dot, old('settings.mail.port') returns null: use old('settings')['mail.port'] ?? $current. docs/settings-screens.md has a complete form.

An empty field is null or "", and it is handled according to the definition:

Setting What an empty field does
encrypted Keeps the stored value.
nullable Removes the stored value, so it falls back to the global value or the default.
Any other A string accepts "". null, and "" for every other type, are reported as a missing value.

Keys that are not in the array are left alone. An unchecked checkbox sends nothing, so add a hidden field with 0 before it.

Everything is checked before anything is written, and the writes and removals run in one database transaction. Errors are thrown as a ValidationException keyed by setting. Keys that are not defined, or that the model may not override, are errors too, even outside strict mode, so request input can be passed in. If the input is not an array, the error is keyed settings. fill() accepts any defined setting of the scope: if a form must only change some of them, pick those keys first.

set() differs on purpose: it stores exactly what you give it, including null and "".

Validating with the rest of a request

When the form has its own field names, or other fields besides settings, use rules(). It takes field name => setting key and returns Laravel validation rules:

With the same field => key map as a second argument, fill() reads the values by field name and ignores the rest of the data, such as title. A field that is missing from the data is left alone.

Nothing is saved by validating.

Rules that depend on another field

The rules of a definition only see their own value, so required_if and required_with do not work there. Add them in the request, next to what rules() returns:

fill() and set() do not check these. For an encrypted setting the rule also fails when the field is left empty to keep the stored value, so add it only while Settings::has() is false.

An empty field keeps an encrypted value. To let a form delete one, call forget() after fill(), with both inside a DB::transaction().

Validation and error messages

Every write is checked against the setting's type, then against its rules. Input is converted where it makes sense: "1", "true" and "on" become true, and "42" becomes 42, so form and JSON input can be passed straight in.

When several values are set at once, they are all validated before anything is written.

Errors are keyed by setting. Their messages use the setting's label and, for a select or enum, the labels of its options.

Messages from your rules use Laravel's validation translations. The package's own messages (type errors and endpoint errors) come in English, Catalan and Spanish, follow the app locale and fall back to the fallback locale, then to English. To change them or add a language, publish them to lang/vendor/settings-kit:

Exceptions that point to a programming mistake, such as an undefined key in strict mode, are always in English.

Laravel configuration

A setting can replace a value of Laravel's configuration, for example to let an administrator change the sender of your emails. Name the config key in the definition:

Nothing changes until you call applyConfig():

Only stored values are written. A setting with nothing stored leaves the configuration as your files and .env define it, and if an earlier call had overwritten it, the original value is restored. For a nullable setting, a stored null or "" counts as nothing stored.

When to call it

Call it right before the service that reads the configuration is built. For mail, in a service provider:

Avoid calling it directly in boot():

For tenant or user values, call it on the model once you know which one it is: $team->settings()->applyConfig(). It writes the model's own value, or else the stored global one.

Settings screens

panel() returns everything a form needs, translated to the current locale:

For a model, the panel only lists settings that model may override. Settings::panel() lists every setting with its global value.

rules leaves out rule objects and closures, which cannot be sent as JSON. The JSON endpoints include it, so a rule such as exists:users,email shows a table name to whoever can read the panel.

The package does not ship any UI: panel() is the data, and your app renders it with whatever it already uses. See docs/settings-screens.md for complete examples with Blade, Livewire and a JavaScript SPA.

Translating labels

Labels, descriptions and option labels come from lang/{locale}/settings.php. Missing entries fall back to a readable version of the key (font_size becomes "Font Size").

Enum options can also provide a label() method on the enum.

Encrypted settings

Settings declared with encrypted are stored encrypted with your APP_KEY. They are meant for API keys and other secrets, so they are treated as write-only outside your own code:

SettingUpdated and SettingReset events do carry the decrypted value, and $event->encrypted is true for them. Check it before logging an event, and remember that a queued listener stores the event, secret included, in your queue.

After changing encrypted or APP_KEY

Stored values are not converted by themselves. Run this after either change:

The command covers global values and model overrides of defined settings. It fails if some value cannot be read, so a deploy script can stop before the old key is removed. --dry-run shows what it would do.

JSON endpoints

They are optional. Register the routes inside your own group, so your auth middleware applies:

Method URI Action
GET /settings?group=… Global panel. Requires the manage-settings ability.
PATCH /settings Update global values. Requires the manage-settings ability.
GET /settings/me?group=… The authenticated user's panel.
PATCH /settings/me Update the user's overrides.
DELETE /settings/me/{key?} Reset one override, or all of them.

Request bodies look like {"values": {"appearance.theme": "dark"}}. Every response returns the updated panel as {"data": [...]}. Unknown or non-overridable keys and invalid values return a standard 422.

PATCH stores values as set() does, with one exception: empty values of encrypted settings are ignored. Sending null for any other nullable setting stores null; use DELETE to remove a user's override.

Global endpoints answer 403 until you define the ability (the name is configurable in http.global_ability):

The write endpoints (PATCH and DELETE) are rate limited with the throttle middleware, 60 requests per minute by default. A single PATCH accepts at most 100 settings, and each value may be at most 10 KB once encoded as JSON; anything bigger returns a 422 before anything is written. Change these in http.throttle ('attempts,minutes', or null to turn it off), http.max_keys and http.max_value_bytes.

Renaming a setting

Stored values belong to a key. When you rename a setting, change its definition and move the values as part of the deploy:

It moves the global value and every override. Run it before settings:prune, which deletes the values of keys that are no longer defined. A scope that already stores the new key keeps that value unless you pass --overwrite. Running the command again does no harm.

Events

SettingUpdated and SettingReset are dispatched once the surrounding database transaction commits. settable is the model for model scopes, and null for global ones or when the scope was given as a type and id.

Unreadable values

A stored value reads as its fallback (the global value or the default) when it cannot be decrypted, or when it no longer fits the definition, for example after changing a type or removing an option. The package then logs a warning and dispatches SettingUnreadable, once per request or job for each scope and key. reason is decryption or invalid. Neither the log nor the event includes the stored value.

To fix it, save the setting again or remove the value with forget(). If encryption is the cause, run settings:encrypt. Set log_unreadable to false in the config to stop the warnings.

Strict mode

By default, keys that were never defined still work with get() and set() as a simple untyped store, and get($key, $default) returns $default when nothing is stored. Set SETTINGS_KIT_STRICT=true to turn any undefined key into an UndefinedSettingException, which catches typos. The $default argument only applies to undefined keys; defined keys always use their declared default.

fill(), rules(), applyConfig() and the JSON endpoints only accept defined keys in both modes.

Artisan commands

Command Description
settings:list {--group=} Definitions with their default, global value and number of overrides.
settings:export {path?} {--all} {--with-secrets} Export global values as JSON (stored values only, or all with --all). Encrypted settings are skipped unless --with-secrets.
settings:import {path} {--dry-run} Validate and import global values from JSON.
settings:prune {--force} Delete values of settings that are no longer defined, and overrides that are no longer allowed.
settings:rename {from} {to} {--overwrite} Move stored values to a new key, in every scope. See Renaming a setting.
settings:encrypt {--decrypt} {--dry-run} Encrypt stored values with the current APP_KEY. See Encrypted settings.
settings:clear-cache Invalidate every cached value.
settings:upgrade-from-v1 Convert a 1.x installation. See UPGRADE.md.

Caching

Values are cached per scope (global, or one model) in your default cache store, forever by default, and invalidated on every write. This works with every cache driver; tags are not required. Configure it in cache (enabled, store, ttl, prefix). The package is safe to use with Octane and queue workers: values are memoised per request and per job, never across them.

Transactions

Wrap writes to several scopes in a normal database transaction when they must succeed or fail together:

A scope written inside a transaction is read straight from the database until the transaction ends, so values that are rolled back never reach the cache. Its cache entry is cleared again after the commit, in case another request cached the old rows in the meantime. SettingUpdated and SettingReset are dispatched after the commit.

Size and cleanup

A user's overrides live in one cache entry per user, and the global values in one more. A request reads the global entry plus the entry of each model it asks about, so the cost does not grow with the number of users, only with the number of settings one user has overridden.

settings:clear-cache and Settings::flushCache() do not delete the old entries: they change the version in the cache key, and the old entries are never read again. With the default ttl of null on a store that does not evict (file, database), they stay until you clear that store. Set cache.ttl to a number of seconds, or use Redis or Memcached, which evict entries that are no longer used.

Upgrading from 1.x

Version 2 is a redesign. See UPGRADE.md for the automated migration command and the API changes.

Contributing

See SECURITY.md.

License

MIT. See LICENSE.md.


All versions of laravel-settings-kit with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
laravel/framework Version ^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package metalinked/laravel-settings-kit contains the following files

Loading the files please wait ...