PHPackages                             x-laravel/eloquent-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. [Database &amp; ORM](/categories/database)
4. /
5. x-laravel/eloquent-settings

ActiveLibrary[Database &amp; ORM](/categories/database)

x-laravel/eloquent-settings
===========================

Add simple but flexible multiple settings to your Laravel models.

v1.0.0(1mo ago)111↓100%MITPHPPHP ^8.2CI passing

Since Apr 12Pushed 1mo agoCompare

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

READMEChangelog (1)Dependencies (5)Versions (2)Used By (0)

Eloquent Settings
=================

[](#eloquent-settings)

[![Tests](https://github.com/x-laravel/eloquent-settings/actions/workflows/tests.yml/badge.svg)](https://github.com/x-laravel/eloquent-settings/actions/workflows/tests.yml)[![PHP](https://camo.githubusercontent.com/187240af044d09d5b14a1d9d9ebdf3f7a993e4c7bc09bdb46b4ba661a891bf5b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322532422d626c7565)](https://www.php.net)[![Laravel](https://camo.githubusercontent.com/42e62a9adb05b6cb16993782fd4b04b64a76be3ff5704d170001885eb70c8448/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d313225323025374325323031332d726564)](https://laravel.com)[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE.md)

A Laravel package that adds simple but flexible JSON-based settings management to Eloquent models.

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

[](#requirements)

- PHP ^8.2
- Laravel ^12.0 | ^13.0

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

[](#installation)

```
composer require x-laravel/eloquent-settings
```

The service provider is registered automatically via Laravel's package discovery.

Setup
-----

[](#setup)

### 1. Migration

[](#1-migration)

Add the settings column to your table using the `settingsBag()` Blueprint macro:

```
Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->settingsBag(); // adds a nullable JSON settings column
    $table->timestamps();
});
```

### 2. Model

[](#2-model)

Add the `HasSettingsBag` trait to your model:

```
use Illuminate\Database\Eloquent\Model;
use XLaravel\EloquentSettings\HasSettingsBag;

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

Usage
-----

[](#usage)

### Get all settings

[](#get-all-settings)

```
$user->settings()->all(); // ['theme' => 'dark', 'lang' => 'tr']
```

### Get a specific setting

[](#get-a-specific-setting)

```
$user->settings()->get('theme');           // 'dark'
$user->settings()->get('theme', 'light'); // default value if missing
$user->settings()->get('layout.boxed');   // dot notation for nested keys
```

### Check if a setting exists

[](#check-if-a-setting-exists)

```
$user->settings()->has('theme');        // true / false
$user->settings()->has('layout.boxed'); // dot notation supported
```

### Add or update a setting

[](#add-or-update-a-setting)

```
$user->settings()->update('theme', 'dark');
$user->settings()->update('layout.boxed', true); // dot notation for nested keys
```

### Delete a setting

[](#delete-a-setting)

```
$user->settings()->delete('theme');        // removes one setting
$user->settings()->delete('layout.boxed'); // dot notation supported
$user->settings()->delete();              // clears all settings
```

### Replace all settings

[](#replace-all-settings)

```
$user->settings()->apply(['theme' => 'dark', 'lang' => 'tr']);
```

All methods except `all()`, `get()`, and `has()` return `$this` for chaining:

```
$user->settings()
    ->update('theme', 'dark')
    ->update('lang', 'tr');
```

Multiple Setting Groups
-----------------------

[](#multiple-setting-groups)

A model can delegate settings to a related model, keeping different concerns separate.

### 1. Create a related settings table

[](#1-create-a-related-settings-table)

```
Schema::create('user_theme_settings', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->settingsBag();
    $table->timestamps();
});
```

### 2. Create the related model

[](#2-create-the-related-model)

```
use Illuminate\Database\Eloquent\Model;
use XLaravel\EloquentSettings\HasSettingsBag;

class UserThemeSetting extends Model
{
    use HasSettingsBag;
}
```

### 3. Define the relationship on the parent model

[](#3-define-the-relationship-on-the-parent-model)

```
class User extends Model
{
    use HasSettingsBag;

    public function themeSettings()
    {
        return $this->hasOne(UserThemeSetting::class);
    }
}
```

### 4. Access the related settings

[](#4-access-the-related-settings)

```
$user->settings('theme')->get('layout.boxed');
$user->settings('theme')->update('color', 'blue');
$user->settings('theme')->delete('color');
```

If the related record does not exist yet, it is created automatically on the first `update()` or `apply()` call.

Default Settings
----------------

[](#default-settings)

Define `$defaultSettings` on your model to provide fallback values when no settings have been saved:

```
class User extends Model
{
    use HasSettingsBag;

    protected $defaultSettings = [
        'theme' => 'light',
        'notifications' => true,
    ];
}
```

```
$user = User::create([]); // settings initialised from $defaultSettings automatically
$user->settings()->get('theme'); // 'light'
```

Allowed Settings
----------------

[](#allowed-settings)

Define `$allowedSettings` to restrict which keys can be persisted. Any other keys are silently dropped on save:

```
class User extends Model
{
    use HasSettingsBag;

    protected $allowedSettings = ['theme', 'language'];
}
```

```
$user->settings()->apply(['theme' => 'dark', 'language' => 'tr', 'other' => 'ignored']);
$user->settings()->all(); // ['theme' => 'dark', 'language' => 'tr']
```

Method Alias
------------

[](#method-alias)

If you prefer a different method name instead of `settings()`, define `$mapSettingsTo`:

```
class User extends Model
{
    use HasSettingsBag;

    protected $mapSettingsTo = 'config';
}
```

```
$user->config()->get('theme');
$user->config('theme')->update('color', 'blue');
```

Testing
-------

[](#testing)

```
# Build first (once per PHP version, or after composer.json changes)
docker compose --profile php82 up --build

# Run tests
docker compose --profile php82 up
docker compose --profile php83 up
docker compose --profile php84 up
docker compose --profile php85 up
```

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](https://opensource.org/license/MIT).

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance89

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

58d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2bdb64c6c087c331b8bd5906bb1aa7eb06bc83af3654a48ba8ab9da365976651?d=identicon)[X-Adam](/maintainers/X-Adam)

---

Top Contributors

[![x-adam](https://avatars.githubusercontent.com/u/60411758?v=4)](https://github.com/x-adam "x-adam (1 commits)")

---

Tags

laravelSettingseloquent

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k29.9M42](/packages/kirschbaum-development-eloquent-power-joins)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.0M84](/packages/mongodb-laravel-mongodb)[spatie/laravel-sluggable

Generate slugs when saving Eloquent models

1.5k12.4M291](/packages/spatie-laravel-sluggable)[psalm/plugin-laravel

Psalm plugin for Laravel

3325.1M337](/packages/psalm-plugin-laravel)[watson/validating

Eloquent model validating trait.

9743.4M53](/packages/watson-validating)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8733.1M23](/packages/yajra-laravel-oci8)

PHPackages © 2026

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