PHPackages                             bogdankharchenko/typed-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. bogdankharchenko/typed-laravel-settings

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

bogdankharchenko/typed-laravel-settings
=======================================

Strongly typed laravel settings.

v1.0.0(4y ago)13881MITPHPPHP ^7.4|^8.0

Since Dec 20Pushed 4y ago1 watchersCompare

[ Source](https://github.com/bogdankharchenko/typed-laravel-settings)[ Packagist](https://packagist.org/packages/bogdankharchenko/typed-laravel-settings)[ RSS](/packages/bogdankharchenko-typed-laravel-settings/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (10)Dependencies (5)Versions (16)Used By (0)

[![run-tests](https://github.com/bogdankharchenko/typed-laravel-settings/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/bogdankharchenko/typed-laravel-settings/actions/workflows/run-tests.yml)[![codecov](https://camo.githubusercontent.com/1b4699ad34f266552d362d0ac793a774c9b16b308d51e10add8571dc35830568/68747470733a2f2f636f6465636f762e696f2f67682f626f6764616e6b6861726368656e6b6f2f74797065642d6c61726176656c2d73657474696e67732f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d384f5345494d304c3138)](https://codecov.io/gh/bogdankharchenko/typed-laravel-settings)

### Strongly Typed Laravel Settings

[](#strongly-typed-laravel-settings)

#### Install

[](#install)

```
composer require bogdankharchenko/typed-laravel-settings
```

#### Model Setup

[](#model-setup)

```
namespace App\Models\User;

use Illuminate\Database\Eloquent\Model;
use BogdanKharchenko\Settings\Models\HasSettings;

class User extends Model
{
    use HasSettings;
}
```

#### Creating a Setting Class

[](#creating-a-setting-class)

This class represents a group of available settings for a given model. Public properties and their values will automatically be serialized into a json column, and serve as defaults.

```
use BogdanKharchenko\Settings\BaseSettings;

class UserSettings extends BaseSettings
{
    public string $favoriteColor = 'red';
}
```

#### Set Settings

[](#set-settings)

Changing the values will persist them into the database. When updating settings, cache will automatically be flushed.

```
$user = User::first();

$user->setSettings(function(UserSettings $settings){
    $settings->favoriteColor = 'blue';
});

// Updating Multiple Settings

$user->setSettings(function(UserSettings $settings, EmailPreferences $emailPreferences){
    $settings->favoriteColor = 'blue';

    $emailPreferences->marketing = false;
});

// Using the `setSettings()` Closure is a wrapper around the code example below.
$settings = new UserSettings($user);

$settings->favoriteColor = 'pink';

$settings->saveSettings();
```

#### Get Settings

[](#get-settings)

Creating a helper function on your allows us to access strongly typed settings. When a setting is retrieved from the database, it will overwrite the default setting on the class.

```
class User extends Model
{
    public function config(): UserSettings
    {
        return new UserSettings($this);
    }
}

$user->config()->favoriteColor // returns blue

// Alternatively you can use docblocks to assist in typing

/** @var UserSettings $settings */
$settings = $user->getSettings(UserSettings::class);

$settings->favoriteColor // returns blue
```

#### Setting Value Encryption

[](#setting-value-encryption)

You may decide to encrypt/decrypt sensitive settings such as secrets. You should specify a `protected array $encrypted` with a list of properties to encrypt/decrypt. Data will be encrypted into the database, and decrypted when retrieved.

```
use BogdanKharchenko\Settings\BaseSettings;

class UserSettings extends BaseSettings
{
    protected array $encrypted = [
        'secret',
        'list',
    ];

    public ?string $secret = null;

    public array $list = [
        'a',
        'b',
        'c',
    ];
}
```

The default encrypter is `BogdanKharchenko\Settings\Repository\Encrypter` but you may choose to implement your own encryption strategy by implementing `BogdanKharchenko\Settings\Contracts\EncrypterInterface` and changing the config.

#### Validation

[](#validation)

You can define validation rules in your custom `Settings` class, in the same way as you would in `FormRequests` using the `rules()` and `messages()` methods. These rules will be triggered immediately before attempting to persist the data into the database.

```
class SimpleSettingWithRule extends BaseSettings
{
    public string $favoriteColor = 'red';

    // Optional
    public function rules()
    {
        return [
            'favoriteColor' => ['required', Rule::in(['red', 'green'])],

            // Or
            $this->toName()->favoriteColor => ['required', Rule::in(['red', 'green'])],
        ];
    }

    // Optional
    public function messages()
    {
        return [
            'favoriteColor.in' => 'color does not match',
        ];
    }
}
```

Validation is optional, as you may choose to do this in other parts of your app. You can also customize the validator by implementing the `BogdanKharchenko\Settings\Contracts\ValidatorInterface` and changing the `validator` config.

#### Morph Map &amp; Caching

[](#morph-map--caching)

Similarly to morph map for Eloquent, it is a good idea to allow your Setting be more flexible to restructuring without touching your database.

```
// config/typed-settings.php

return [
    'morph'=>[
        'user-settings'=> UserSettings::class,
    ],
    'cache' => [
        'enabled' => true,
        'store' => 'redis',
        'seconds' => 600,
    ],

   'encrypter' => \BogdanKharchenko\Settings\Repository\Encrypter::class,
   'validator' => \BogdanKharchenko\Settings\Repository\Validator::class,
];
```

#### Scopes

[](#scopes)

Sometimes you may need to check a setting on a database level, there is a helper scope available.

```
$users = User::query()
    ->whereSettings(UserSettings::class, 'favoriteColor', 'blue')
    ->get();
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~2 days

Total

8

Last Release

1589d ago

Major Versions

0.10 → v1.0.02022-01-04

### Community

Maintainers

![](https://www.gravatar.com/avatar/d8358134d14f84af9f0604aa6774be0ac479466cfd6f8cf101217ba02def3300?d=identicon)[bogdankharchenko](/maintainers/bogdankharchenko)

---

Top Contributors

[![bogdankharchenko](https://avatars.githubusercontent.com/u/32746389?v=4)](https://github.com/bogdankharchenko "bogdankharchenko (51 commits)")

---

Tags

laravelsettings

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[kirschbaum-development/commentions

A package to allow you to create comments, tag users and more

12369.2k](/packages/kirschbaum-development-commentions)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[glhd/special

1929.4k](/packages/glhd-special)[bjuppa/laravel-blog

Add blog functionality to your Laravel project

483.3k2](/packages/bjuppa-laravel-blog)

PHPackages © 2026

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