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

ActiveLibrary

petermeijer/laravel-model-settings
==================================

03↓100%PHP

Since Mar 17Pushed 4d agoCompare

[ Source](https://github.com/peter-meijer/laravel-model-settings)[ Packagist](https://packagist.org/packages/petermeijer/laravel-model-settings)[ RSS](/packages/petermeijer-laravel-model-settings/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Model Settings
======================

[](#laravel-model-settings)

A flexible and type-safe settings package for Laravel models. This package allows you to attach settings to any Eloquent model without adding columns to your table. It supports different groups, types (string, boolean, integer, float, array), and custom definitions using Enums or classes.

Features
--------

[](#features)

- **Eloquent Integration**: Attach settings to any model via a simple trait.
- **Type Safety**: Built-in support for multiple data types with automatic casting.
- **Organization**: Group settings to keep things organized.
- **Enum Support**: Use PHP Enums to define your settings for better IDE support and type safety.
- **Performance**: Eager-loads settings to avoid N+1 query problems.
- **Configurable**: Easily customize default groups and type aliases.

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

[](#installation)

You can install the package via composer:

```
composer require petermeijer/laravel-model-settings
```

The service provider will automatically register itself.

You should publish and run the migrations with:

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

You can publish the config file with:

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

Usage
-----

[](#usage)

### 1. Prepare your Model

[](#1-prepare-your-model)

Add the `HasSettingsInterface` and `HasSettings` trait to your model:

```
use Illuminate\Database\Eloquent\Model;
use PeterMeijer\LaravelModelSettings\Concerns\HasSettings;
use PeterMeijer\LaravelModelSettings\Contracts\HasSettingsInterface;

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

### 2. Basic Usage (String keys)

[](#2-basic-usage-string-keys)

You can set and get settings using simple string keys. The type will be inferred when setting the value.

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

// Set settings
$user->settings()->set('theme', 'dark');
$user->settings()->set('notifications_enabled', true);
$user->settings()->set('login_count', 5);

// Get settings
$theme = $user->settings()->string('theme'); // 'dark'
$enabled = $user->settings()->boolean('notifications_enabled'); // true
$logins = $user->settings()->integer('login_count'); // 5

// Check existence
if ($user->settings()->has('theme')) {
    // ...
}
```

### 3. Using Settings Groups

[](#3-using-settings-groups)

You can organize settings into groups:

```
$user->settings('profile')->set('color', 'blue');
$color = $user->settings('profile')->string('color'); // 'blue'

// Or via the fluent API
$user->settings()->group('profile')->set('color', 'red');
```

### 4. Advanced Usage (Setting Definitions)

[](#4-advanced-usage-setting-definitions)

For better type safety and organization, you can define settings using Enums or classes that implement `ModelSettingDefinition`.

```
use PeterMeijer\LaravelModelSettings\Contracts\ModelSettingDefinition;

enum UserSetting: string implements ModelSettingDefinition
{
    case Theme = 'theme';
    case Notifications = 'notifications';

    public function key(): string { return $this->value; }
    public function type(): string {
        return match($this) {
            self::Theme => 'string',
            self::Notifications => 'boolean',
        };
    }
    public function group(): string { return 'default'; }
}

// Usage with Enums
$user->settings()->set(UserSetting::Theme, 'light');
$theme = $user->settings()->string(UserSetting::Theme);
```

### 5. Available Methods

[](#5-available-methods)

The `settings()` accessor provides many helpful methods (inspired by Laravel's `InteractsWithData` trait):

- `set($key, $value)`
- `string($key, $default = '')`
- `boolean($key, $default = false)`
- `integer($key, $default = 0)`
- `float($key, $default = 0.0)`
- `array($key)`
- `date($key, $format = null, $tz = null)`
- `enum($key, $enumClass, $default = null)`
- `str($key)` (returns `Illuminate\Support\Stringable`)
- `has($key)`
- `missing($key)`
- `all()`
- `only($keys)`
- `except($keys)`

Configuration
-------------

[](#configuration)

The configuration file allows you to change the default group and define type aliases:

```
return [
    'default_group' => 'default',

    'type_mappings' => [
        'default' => 'string',
        'types' => [
            'boolean' => ['bool', 'boolean'],
            'integer' => ['int', 'integer'],
            // ...
        ],
    ],
];
```

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

22

↑

LowBetter than 25% of packages

Maintenance65

Regular maintenance activity

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/2a3fc61a6e4ce2c25bf2625472d0c6472adf6a84c507c0daea907225027d76d2?d=identicon)[peter-meijer](/maintainers/peter-meijer)

---

Top Contributors

[![peter-meijer](https://avatars.githubusercontent.com/u/193896?v=4)](https://github.com/peter-meijer "peter-meijer (3 commits)")

### Embed Badge

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

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

PHPackages © 2026

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