PHPackages                             arthurydalgo/lara-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. arthurydalgo/lara-settings

ActiveLibrary

arthurydalgo/lara-settings
==========================

Settings for Laravel.

1.0(3y ago)017MITPHPPHP ^8.1|^8.2

Since Apr 6Pushed 2y agoCompare

[ Source](https://github.com/ArthurYdalgo/lara-settings)[ Packagist](https://packagist.org/packages/arthurydalgo/lara-settings)[ RSS](/packages/arthurydalgo-lara-settings/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)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
```

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

[](#basic-usage)

```
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,
        ];
    }
}
```

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

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 72.7% 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

Unknown

Total

1

Last Release

1129d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/293828dcf9a77a46773240833216ac450a33aa638567381d5fc60fd7b4ace155?d=identicon)[ArthurYdalgo](/maintainers/ArthurYdalgo)

---

Top Contributors

[![npabisz](https://avatars.githubusercontent.com/u/13676368?v=4)](https://github.com/npabisz "npabisz (8 commits)")[![ArthurYdalgo](https://avatars.githubusercontent.com/u/25585428?v=4)](https://github.com/ArthurYdalgo "ArthurYdalgo (2 commits)")[![janar153](https://avatars.githubusercontent.com/u/712648?v=4)](https://github.com/janar153 "janar153 (1 commits)")

---

Tags

laravelSettings

### Embed Badge

![Health badge](/badges/arthurydalgo-lara-settings/health.svg)

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

###  Alternatives

[spatie/laravel-health

Monitor the health of a Laravel application

85810.0M83](/packages/spatie-laravel-health)[watson/validating

Eloquent model validating trait.

9723.3M46](/packages/watson-validating)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

423715.4k1](/packages/clickbar-laravel-magellan)[yadahan/laravel-authentication-log

Laravel Authentication Log provides authentication logger and notification for Laravel.

416632.8k5](/packages/yadahan-laravel-authentication-log)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[outerweb/settings

Application wide settings stored in your database

4899.2k5](/packages/outerweb-settings)

PHPackages © 2026

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