PHPackages                             npabisz/laravel-settings - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. npabisz/laravel-settings

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

npabisz/laravel-settings
========================

Settings for Laravel.

v1.2.0(2mo ago)277.1k↑327.3%9MITPHPPHP ^8.1|^8.2|^8.3|^8.4

Since Dec 1Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/npabisz/laravel-settings)[ Packagist](https://packagist.org/packages/npabisz/laravel-settings)[ RSS](/packages/npabisz-laravel-settings/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (10)Versions (20)Used By (0)

Settings for Laravel
====================

[](#settings-for-laravel)

Basic settings for Laravel 9+. Can be either global or morphed to models.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Laravel &gt;= 9.0

Installation
------------

[](#installation)

```
composer require npabisz/laravel-settings
```

Then publish vendor resources and migration

```
php artisan vendor:publish --provider="Npabisz\LaravelSettings\SettingsServiceProvider"
```

Finally, you should run migration

```
php artisan migrate
```

Using a custom Setting model location (DDD)
-------------------------------------------

[](#using-a-custom-setting-model-location-ddd)

By default the package looks for `App\Models\Setting`. If your project keeps the model elsewhere — for example inside a DDD domain folder like `App\Domain\Settings\Models\Setting` — publish the config and point it at your class:

```
php artisan vendor:publish --tag=settings-config
```

```
// config/settings.php
return [
    'model' => \App\Domain\Settings\Models\Setting::class,
];
```

The class must extend `Npabisz\LaravelSettings\Models\AbstractSetting`. No database changes are required — `settingable_type` stores the *owner* model FQCN, not the Setting FQCN, so relocating the Setting class is transparent.

Basic usage
-----------

[](#basic-usage)

Example of `User` model which uses `HasSettings` trait is simple as that:

```
// Get value of `is_gamer` setting
$user->settings->get('is_gamer');

// Set value of `games_count` setting
$user->settings->set('games_count', 10);

// Get user address object
$address = $user->settings->get('address');
echo "User is from $address->city";
```

You can use it with enums

```
enum Settings: string {
    case ApiMode = 'api_mode';
    case Enabled = 'enabled';
}

enum ApiMode: string {
    case Production = 'production';
    case Sandbox = 'sandbox';
}

$user->settings->set(Settings::ApiMode, ApiMode::Production);
```

You are not limited to `User` model, this works on every model:

```
// Check if article is premium
$article->settings->get('is_premium');
```

There are also different ways and scopes to access:

```
use Npabisz\LaravelSettings\Facades\Settings;

// Get global website setting value
$value = Settings::get('api_mode');

// Update global website setting value
Settings::set('api_mode', 'production');

// Access to the Setting model
$settingModel = Settings::setting('api_mode');
$settingModel->delete();

// Get all global website settings models
$settingModels = Settings::all();

// Get all global website settings models,
// but filling the missing ones with default values
$settingModels = Settings::allWithDefaults();

foreach ($settingModels as $setting) {
    if (null === $setting->id) {
        // This one isn't existing in database
        // and has default value based on definition
    }
}
```

Scoping to models
-----------------

[](#scoping-to-models)

```
use Npabisz\LaravelSettings\Facades\Settings;

// Local scope for model
$model = User::first();
$userSettings = Settings::scope($model);

// Get user setting value
$value = $userSettings->get('is_newsletter_opted_in');

// Set user setting value
$userSettings->set('is_newsletter_opted_in', true);

// You can use any model which implements HasSettings trait
// Local scope for article
$article = Article::first();
$articleSettings = Settings::scope($article);

// Get article setting value
$articleSettings->get('enable_promo_banner');

// Set article setting value
$articleSettings->set('enable_promo_banner', true);
```

Using global scopes
-------------------

[](#using-global-scopes)

It easier to use global scope for models that won't change during request, eg. logged in user. Which is globally scoped by default, but here is example how to do it manually, eg. you need to persist scope in command.

```
use Npabisz\LaravelSettings\Facades\Settings;

// Global scope for model
$user = User::first();
Settings::scopeGlobal($user);

// Now you can call magic method and
// it will return SettingsContainer
// for scoped user
Settings::user()->get('is_gamer');
Settings::user()->set('is_gamer', false);

// Replace scope
$anotherUser = User::find(2);
Settings::scopeGlobal($anotherUser);

// Now settings returned by user()
// method belongs to $anotherUser
Settings::user()->set('is_gamer', true);

// You can scope any model which
// has HasSettings trait
$article = Article::first();
Settings::scopeGlobal($article);

// Now you can access them via
// magic method named after class name
Settings::article()->get('is_premium');
```

Settings definitions
--------------------

[](#settings-definitions)

Every setting has to have definition. This way it is always of the same type and can have default values. Settings definitions are declared under static method `getSettingsDefinitions`.

> ### Remember
>
> [](#remember)
>
> Global settings are defined on `Setting` model.

```
use Npabisz\LaravelSettings\Models\AbstractSetting;

class Setting extends AbstractSetting
{
    /**
     * @return array
     */
    public static function getSettingsDefinitions (): array
    {
        return [
            [
                // Setting name which will be unique
                'name' => 'api_mode',
                // Default value for setting
                'default' => 'sandbox',
                // You can optionally specify valid values
                'options' => [
                    'production',
                    'sandbox',
                ],
            ],
            [
                // Another setting name
                'name' => 'is_enabled',
                // You can optionally specify setting cast
                'cast' => 'bool',
                // Default value for setting
                'default' => false,
            ],
            [
                // Another setting name
                'name' => 'address',
                // You can use classes which will be stored as json
                'cast' => Address::class,
            ],
        ];
    }
}
```

Instead of storing each field of address in separate setting. You can use class which will be then casted to json.

```
use Npabisz\LaravelSettings\Models\BaseSetting;

class Address extends BaseSetting
{
    /**
     * @var string
     */
    public string $street;

    /**
     * @var string
     */
    public string $zipcode;

    /**
     * @var string
     */
    public string $city;

    public function __construct ()
    {
        // You can specify default values
        $this->street = '';
        $this->zipcode = '';
        $this->city = '';
    }

    /**
     * This method will be used to populate
     * data from json object.
     *
     * @param array $data
     */
    public function fromArray (array $data)
    {
        $this->street = $data['street'] ?? '';
        $this->zipcode = $data['zipcode'] ?? '';
        $this->city = $data['city'] ?? '';
    }

    /**
     * @return array
     */
    public function toArray (): array
    {
        return [
            'street' => $this->street,
            'zipcode' => $this->zipcode,
            'city' => $this->city,
        ];
    }
}
```

Example of using enums

```
enum Settings: string {
    case ApiMode = 'api_mode';
    case Enabled = 'enabled';
}
```

```
enum ApiMode: string {
    case Production = 'production';
    case Sandbox = 'sandbox';
}
```

```
use Npabisz\LaravelSettings\Models\AbstractSetting;

use App\Enums\Settings;
use App\Enums\ApiMode;

class Setting extends AbstractSetting
{
    /**
     * @return array
     */
    public static function getSettingsDefinitions (): array
    {
        return [
            [
                'name' => Settings::ApiMode,
                'default' => ApiMode::Sandbox,
                'enum' => ApiMode::class,
            ],
            [
                'name' => Settings::Enabled,
                'cast' => 'bool',
                'default' => false,
            ]
        ];
    }
}
```

Example of `nullable` setting

```
use Npabisz\LaravelSettings\Models\AbstractSetting;

class Setting extends AbstractSetting
{
    /**
     * @return array
     */
    public static function getSettingsDefinitions (): array
    {
        return [
            [
                'name' => 'display_name',
                'is_nullable' => true,
            ]
        ];
    }
}
```

Models
------

[](#models)

If you want to have settings on model you need to use `HasSettings` trait and declare some settings definitions.

```
use Npabisz\LaravelSettings\Traits\HasSettings;

class User extends Authenticatable
{
    use HasSettings;

    ...

    /**
     * @return array
     */
    public static function getSettingsDefinitions(): array
    {
        return [
            [
                'name' => 'theme_mode',
                'default' => 'light',
                'options' => [
                    'light',
                    'dark',
                ],
            ],
            [
                'name' => 'is_gamer',
                'cast' => 'bool',
            ],
            [
                'name' => 'games_count',
                'cast' => 'int',
            ],
        ];
    }
}
```

Now you can get their settings.

```
use Npabisz\LaravelSettings\Facades\Settings;

// Assuming user is logged in
Settings::user()->get('is_gamer');
Settings::user()->set('games_count', 10);

// You can also access settings via property
$user->settings->get('is_gamer');
$user->settings->set('games_count', 10);
```

License
-------

[](#license)

`npabisz/laravel-settings` is released under the MIT License. See the bundled [LICENSE](./LICENSE) for details.

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance83

Actively maintained with recent releases

Popularity36

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 85.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~70 days

Recently: every ~13 days

Total

19

Last Release

85d ago

PHP version history (4 changes)v1.0.0PHP ^8.1

v1.0.7PHP ^8.1|^8.2

v1.0.8PHP ^8.1|^8.2|^8.3

v1.0.12PHP ^8.1|^8.2|^8.3|^8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/9768121?v=4)[yorki](/maintainers/yorki)[@Yorki](https://github.com/Yorki)

---

Top Contributors

[![npabisz](https://avatars.githubusercontent.com/u/13676368?v=4)](https://github.com/npabisz "npabisz (23 commits)")[![chaser79](https://avatars.githubusercontent.com/u/30496230?v=4)](https://github.com/chaser79 "chaser79 (2 commits)")[![Bongo9911](https://avatars.githubusercontent.com/u/61335439?v=4)](https://github.com/Bongo9911 "Bongo9911 (1 commits)")[![janar153](https://avatars.githubusercontent.com/u/712648?v=4)](https://github.com/janar153 "janar153 (1 commits)")

---

Tags

laravelSettings

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/npabisz-laravel-settings/health.svg)

```
[![Health](https://phpackages.com/badges/npabisz-laravel-settings/health.svg)](https://phpackages.com/packages/npabisz-laravel-settings)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.4M354](/packages/psalm-plugin-laravel)[laravel/ai

The official AI SDK for Laravel.

1.1k4.6M293](/packages/laravel-ai)[api-platform/laravel

API Platform support for Laravel

58190.1k21](/packages/api-platform-laravel)[illuminate/queue

The Illuminate Queue package.

20433.0M1.8k](/packages/illuminate-queue)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45844.8k1](/packages/pressbooks-pressbooks)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5226.7k](/packages/simplestats-io-laravel-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
