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

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

x-laravel/model-settings-bag
============================

Add simple but flexible multiple settings to your Laravel models.

v3.0.0(10mo ago)0431MITPHPPHP &gt;=7.4

Since Dec 27Pushed 10mo agoCompare

[ Source](https://github.com/X-Laravel/model-settings-bag)[ Packagist](https://packagist.org/packages/x-laravel/model-settings-bag)[ RSS](/packages/x-laravel-model-settings-bag/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (10)Used By (0)

Laravel Str Facade TR Extend
============================

[](#laravel-str-facade-tr-extend)

[![Latest Stable Version](https://camo.githubusercontent.com/64266032fa279567466b560be8bbda52726ae46a573f32d73510518c9ea57484/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f582d4c61726176656c2f6d6f64656c2d73657474696e67732d626167)](https://packagist.org/packages/X-Laravel/model-settings-bag)[![Total Downloads](https://camo.githubusercontent.com/9699cf0f07940e2e551f21e90e259dd7cc087233f7f80151502f3192c2925a17/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f582d4c61726176656c2f6d6f64656c2d73657474696e67732d626167)](https://packagist.org/packages/X-Laravel/model-settings-bag)[![Dependents](https://camo.githubusercontent.com/cefd0352d0fc3df0dca8b810cd69485d5d31cd72c2c893cf482e92baeecbd666/68747470733a2f2f706f7365722e707567782e6f72672f582d4c61726176656c2f6d6f64656c2d73657474696e67732d6261672f646570656e64656e74732e737667)](https://packagist.org/packages/X-Laravel/model-settings-bag)[![License](https://camo.githubusercontent.com/36a9e3888a2d9064a0dd6e907434c67a7784159157a491e53196dc5aed01076d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f582d4c61726176656c2f6d6f64656c2d73657474696e67732d626167)](https://packagist.org/packages/X-Laravel/model-settings-bag)

[![](https://camo.githubusercontent.com/bea0ccfe24d23e27d139f23e7d1d207018c1282e7a7a93e0a6765cc34174b2ee/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f582d4c61726176656c2f6d6f64656c2d73657474696e67732d6261672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572 "Scrutinizer Code Quality")](https://scrutinizer-ci.com/g/X-Laravel/model-settings-bag/build-status/master)[![StyleCI](https://camo.githubusercontent.com/d97747e87b2cf399504ba18d2bf0cdb52b92fc9c1080f306317fa578ebf3375d/68747470733a2f2f7374796c6563692e696f2f7265706f732f3332323733333335302f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/322733350)

Introduction
------------

[](#introduction)

Add simple but flexible multiple settings to your Laravel models.

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

[](#requirements)

PHP &gt;=7.0. Other than that, this library has no requirements.

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

[](#installation)

```
$ composer require x-laravel/model-settings-bag:"~1.0"
```

Integration
-----------

[](#integration)

### Single

[](#single)

#### 1. Add a JSON settings field to your model's migration.

[](#1-add-a-json-settings-field-to-your-models-migration)

*create\_users\_table.php*

```
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->json('settings')->nullable();
    $table->rememberToken();
    $table->timestamps();
});
```

#### 2. Use the trait `XLaravel\ModelSettingsBag\HasSettingsBag` within your model.

[](#2-use-the-trait-xlaravelmodelsettingsbaghassettingsbag-within-your-model)

*User.php*

```
use XLaravel\ModelSettingsBag\HasSettingsBag;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasSettingsBag;

    // truncated for brevity..
}
```

### Multiple

[](#multiple)

#### 1. Add a JSON settings field to your model's migration.

[](#1-add-a-json-settings-field-to-your-models-migration-1)

*create\_user\_theme\_settings\_table.php*

```
Schema::create('user_theme_settings', function (Blueprint $table) {
    $table->id();
    $table->unsignedInteger('user_id');
    $table->json('settings')->nullable();
    $table->rememberToken();
    $table->timestamps();
});
```

#### 2. Use the trait `XLaravel\ModelSettingsBag\HasSettingsBag` within your other setting model.

[](#2-use-the-trait-xlaravelmodelsettingsbaghassettingsbag-within-your-other-setting-model)

*UserThemeSetting.php*

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

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

#### 3. Use the trait `XLaravel\ModelSettingsBag\HasSettingsBag` within your model.

[](#3-use-the-trait-xlaravelmodelsettingsbaghassettingsbag-within-your-model)

*User.php*

```
class User extends Model
{
    use HasSettingsBag;

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

Usage
-----

[](#usage)

### 1.) Get all of the model's settings.

[](#1-get-all-of-the-models-settings)

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

$user->settings()->all();    // Returns an array of the user's settings.
$user->settings('theme')->get();    // Returns an array of a user's theme settings.
```

### 2.) Get a specific setting.

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

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

$user->settings()->get('some.setting');
$user->settings()->get('some.setting', $defaultValue); // With a default value.

$user->settings('theme')->get('layout.boxed');
$user->settings('theme')->get('layout.boxed', $defaultValue); // With a default value.
```

### 3.) Add or update a setting.

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

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

$user->settings()->update('some.setting', 'new value');
$user->settings('theme')->update('layout.boxed', 'new value');
```

### 4.) Determine if the model has a specific setting.

[](#4-determine-if-the-model-has-a-specific-setting)

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

$user->settings()->has('some.setting');
$user->settings('theme')->has('layout.boxed');
```

### 5.) Remove a setting from a model.

[](#5-remove-a-setting-from-a-model)

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

$user->settings()->delete('some.setting');
$user->settings('theme')->delete('layout.boxed');
```

### 6.) Set the default settings for a new model.

[](#6-set-the-default-settings-for-a-new-model)

If you define `$defaultSettings` as an array property on your model, we will use its value as the default settings for any new models that are created *without* settings.

*User.php*

```
use XLaravel\ModelSettingsBag\HasSettingsBag;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasSettingsBag;

    /**
     * The model's default settings.
     *
     * @var array
     */
    protected $defaultSettings = [
    	'homepage' => '/profile'
    ];

    // truncated for brevity..
}
```

### 7.) Specify the settings that are allowed.

[](#7-specify-the-settings-that-are-allowed)

If you define `$allowedSettings` as an array property then only settings which match a value within the `$allowedSettings` array will be saved on the model.

*User.php*

```
use XLaravel\ModelSettingsBag\HasSettingsBag;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasSettingsBag;

    /**
     * The model's allowed settings.
     *
     * @var array
     */
    protected $allowedSettings = ['homepage'];

    // truncated for brevity..
}
```

### 8.) Using another method name other than settings()

[](#8-using-another-method-name-other-than-settings)

If you prefer to use another name other than `settings` , you can do so by defining a `$mapSettingsTo` property. This simply maps calls to the method (such as `config()`) to the `settings()` method.

*User.php*

```
use XLaravel\ModelSettingsBag\HasSettingsBag;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasSettingsBag;

    /**
     * The settings field name.
     *
     * @var string
     */
    protected $mapSettingsTo = 'config';

    // truncated for brevity..
}
```

License
-------

[](#license)

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

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance54

Moderate activity, may be stable

Popularity13

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity59

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

Every ~277 days

Recently: every ~415 days

Total

7

Last Release

306d ago

Major Versions

1.x-dev → 2.x-dev2021-01-09

v2.1.0 → v3.0.02025-07-16

PHP version history (2 changes)v2.0.0PHP &gt;=7.4

v1.0.0PHP &gt;=7.0

### 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 (6 commits)")

### Embed Badge

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

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

###  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

20917.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)
