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

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

bhhaskin/laravel-settings
=========================

Typed, polymorphic key-value settings for Laravel models.

0.3.0(1mo ago)020MITPHPPHP ^8.1

Since Apr 17Pushed 1mo agoCompare

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

READMEChangelog (3)Dependencies (5)Versions (4)Used By (0)

laravel-settings
================

[](#laravel-settings)

Typed, polymorphic key-value settings for Laravel models.

Settings are stored in a dedicated `settings` table with a `type` column so values round-trip as the right PHP type (`boolean`, `integer`, `float`, `string`, `array`, `json`, `datetime`) rather than opaque JSON blobs. The relation is polymorphic, so any model — not just `User` — can have settings.

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

[](#installation)

```
composer require bhhaskin/laravel-settings
```

Publish and run the migration:

```
php artisan vendor:publish --tag=settings-migrations
php artisan migrate
```

Optionally publish the config:

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

Setup
-----

[](#setup)

Add the `HasSettings` trait to any model that should own settings:

```
use Bhhaskin\LaravelSettings\Concerns\HasSettings;
use Illuminate\Foundation\Auth\User as Authenticatable;

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

Usage
-----

[](#usage)

```
$user->setSetting('theme', 'dark');               // type detected as string
$user->setSetting('notifications.email', true);   // type detected as boolean
$user->setSetting('digest.hour', 8);              // integer
$user->setSetting('tags', ['news', 'tips']);      // array

$user->getSetting('theme');                       // 'dark'
$user->getSetting('notifications.email');         // true
$user->getSetting('missing', 'fallback');         // 'fallback'

$user->hasSetting('theme');                       // true
$user->forgetSetting('theme');
$user->allSettings();                             // ['notifications.email' => true, ...]
```

### Forcing a type

[](#forcing-a-type)

Pass a third argument when you want to override auto-detection:

```
use Bhhaskin\LaravelSettings\Enums\SettingType;

$user->setSetting('flag', 'yes', SettingType::Boolean);  // stored as true
$user->setSetting('opens_at', '2026-05-01T09:00:00Z', SettingType::Datetime);
```

### Declaring defaults and types

[](#declaring-defaults-and-types)

Predeclare keys in `config/settings.php` to skip passing types and get defaults for unset keys:

```
'definitions' => [
    'theme'                => ['type' => 'string',  'default' => 'system'],
    'notifications.email'  => ['type' => 'boolean', 'default' => true],
    'digest.hour'          => ['type' => 'integer', 'default' => 8],
],
```

Keys outside this list still work — definitions are opt-in sugar, not a schema.

### Runtime-configurable defaults

[](#runtime-configurable-defaults)

Config defaults ship with your code. When you also need defaults an admin can change at runtime, use the `setting_defaults` table. Defaults can be global or scoped to a specific owner morph class:

```
use Bhhaskin\LaravelSettings\SettingsManager;
use Bhhaskin\LaravelSettings\Facades\Settings;

// Global default — applies to every model that owns settings
Settings::setDefault('theme', 'sunrise');

// Default scoped to one owner class
Settings::setDefault('theme', 'midnight', null, User::class);

Settings::getDefault('theme');               // 'sunrise'
Settings::getDefault('theme', User::class);  // 'midnight'
Settings::forgetDefault('theme', User::class);
```

`getSetting()` resolves in this order:

1. The owner's own stored value
2. DB default scoped to the owner's morph class
3. Global DB default
4. The `$default` argument
5. Config `definitions[$key]['default']`
6. `null`

Use `allSettingsWithDefaults()` when you need the merged view:

```
$user->allSettingsWithDefaults();
// ['theme' => 'midnight', 'digest.hour' => 8, ...]
```

`allSettings()` is unchanged — it still returns only values stored for the owner.

### Attaching settings to other models

[](#attaching-settings-to-other-models)

Because the relation is polymorphic, you can add `HasSettings` to workspaces, teams, or any other model and call the same API. Each model's settings are scoped by `owner_type` / `owner_id`.

Testing
-------

[](#testing)

```
composer test
```

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance90

Actively maintained with recent releases

Popularity8

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 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 ~1 days

Total

3

Last Release

51d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4709778?v=4)[Bryan Haskin](/maintainers/bhhaskin)[@bhhaskin](https://github.com/bhhaskin)

---

Top Contributors

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

###  Code Quality

TestsPest

### Embed Badge

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

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3325.1M337](/packages/psalm-plugin-laravel)[wearepixel/laravel-cart

A cart implementation for Laravel

1355.6k](/packages/wearepixel-laravel-cart)[aedart/athenaeum

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

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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