PHPackages                             lukasss93/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. lukasss93/laravel-model-settings

ActiveLaravel-package[Utility &amp; Helpers](/categories/utility)

lukasss93/laravel-model-settings
================================

Model Settings for your Laravel app

6.2.0(2y ago)43111MITPHPPHP ^8.0

Since Oct 27Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Lukasss93/laravel-model-settings)[ Packagist](https://packagist.org/packages/lukasss93/laravel-model-settings)[ RSS](/packages/lukasss93-laravel-model-settings/feed)WikiDiscussions master Synced 1mo ago

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

[![logo](https://camo.githubusercontent.com/c71d0fdc933b6b2954b4ce8d3a4c093b288dfdb953a18419ea36842e3f9ce7f7/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304d6f64656c25323053657474696e67732e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6c756b6173737339332532466c61726176656c2d6d6f64656c2d73657474696e6773267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d4d6f64656c2b53657474696e67732b666f722b796f75722b4c61726176656c2b617070266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d636f67)](https://camo.githubusercontent.com/c71d0fdc933b6b2954b4ce8d3a4c093b288dfdb953a18419ea36842e3f9ce7f7/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304d6f64656c25323053657474696e67732e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6c756b6173737339332532466c61726176656c2d6d6f64656c2d73657474696e6773267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d4d6f64656c2b53657474696e67732b666f722b796f75722b4c61726176656c2b617070266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d636f67)

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

[](#laravel-model-settings)

[![version](https://camo.githubusercontent.com/021310bbbe9d2b49b810bb6c01fd2feac52b00140e92b9b3d7fdcf70c99d26ee/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c756b6173737339332f6c61726176656c2d6d6f64656c2d73657474696e6773)](https://camo.githubusercontent.com/021310bbbe9d2b49b810bb6c01fd2feac52b00140e92b9b3d7fdcf70c99d26ee/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c756b6173737339332f6c61726176656c2d6d6f64656c2d73657474696e6773)[![downloads](https://camo.githubusercontent.com/953b678a2738ee2b424079b6f1724da347ac3a9cf603acf35a913c0408d9baca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c756b6173737339332f6c61726176656c2d6d6f64656c2d73657474696e67733f636f6c6f723d726564)](https://camo.githubusercontent.com/953b678a2738ee2b424079b6f1724da347ac3a9cf603acf35a913c0408d9baca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c756b6173737339332f6c61726176656c2d6d6f64656c2d73657474696e67733f636f6c6f723d726564)[![license](https://camo.githubusercontent.com/c5e3a66189da80aeec7b792643e766398c27c1b974920ed241d6941c44cd4c1d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c756b6173737339332f6c61726176656c2d6d6f64656c2d73657474696e6773)](https://camo.githubusercontent.com/c5e3a66189da80aeec7b792643e766398c27c1b974920ed241d6941c44cd4c1d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c756b6173737339332f6c61726176656c2d6d6f64656c2d73657474696e6773)[![php](https://camo.githubusercontent.com/e41bc204da885b3bfd711219321422021488bb6d85009cd84216ca525ea6ca72/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c756b6173737339332f6c61726176656c2d6d6f64656c2d73657474696e6773)](https://camo.githubusercontent.com/e41bc204da885b3bfd711219321422021488bb6d85009cd84216ca525ea6ca72/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c756b6173737339332f6c61726176656c2d6d6f64656c2d73657474696e6773)

[![Test](https://github.com/Lukasss93/laravel-model-settings/actions/workflows/test.yml/badge.svg)](https://github.com/Lukasss93/laravel-model-settings/actions/workflows/test.yml)[![Maintainability](https://camo.githubusercontent.com/ba9c18cefb5afd4128a97376ee7c1a8f8c630d5d94a14fa9446917c87b98b53f/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36363762656265346133326265303831333861322f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/Lukasss93/laravel-model-settings/maintainability)[![Test Coverage](https://camo.githubusercontent.com/a93e55b1dae568310fda81a614035959d08d88a11a93fe21151fdda214c4833c/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f36363762656265346133326265303831333861322f746573745f636f766572616765)](https://codeclimate.com/github/Lukasss93/laravel-model-settings/test_coverage)

> Model Settings for your Laravel app

The package requires PHP ^8.0 and follows the FIG standards PSR-1, PSR-2, PSR-4 and PSR-12 to ensure a high level of interoperability between shared PHP.

Bug reports, feature requests, and pull requests can be submitted by following our [Contribution Guide](.github/CONTRIBUTING.md).

⚠️ Forked repository
--------------------

[](#️-forked-repository)

This package is a fork of [glorand/laravel-model-settings](https://github.com/glorand/laravel-model-settings) and contains some breaking changes to make it more flexible and easier to use.

### List of changes between this package and the original one:

[](#list-of-changes-between-this-package-and-the-original-one)

- Added ability to initialize settings at model creation
- Readded **Laravel 8** support
- Converted `defaultSettings` property to **method**
- Converted `settingsRules` property to **method**
- Converted **PHPUnit** tests to **PestPHP** tests
- Renamed `Glorand\Model\Settings` namespace to `Lukasss93\ModelSettings`
- Removed `defaultSettings` from `model_settings.php` config file

🚀 Installation
--------------

[](#-installation)

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

⚙ Publishing the config file
----------------------------

[](#-publishing-the-config-file)

Publishing the config file is optional:

```
php artisan vendor:publish --provider="Lukasss93\ModelSettings\ModelSettingsServiceProvider"
```

🌈 Update your Eloquent Models
-----------------------------

[](#-update-your-eloquent-models)

Your models should use the `HasSettingsField` or `HasSettingsTable` trait.

#### Option 1 - `HasSettingsField` trait

[](#option-1---hassettingsfield-trait)

Run the `php artisan model-settings:model-settings-field` in order to create a migration file for a table.
This command will create a json field (default name `settings`, from config) for the mentioned table.
The default name of the field is `settings`; change the config or env value `MODEL_SETTINGS_FIELD_NAME` if you want to rewrite the default name (**before you run the command!**)

You can choose another than default, in this case you have to specify it in you model.

```
public $settingsFieldName = 'user_settings';
```

Complete example:

```
use Lukasss93\ModelSettings\Traits\HasSettingsField;

class User extends Model
{
    use HasSettingsField;

    //define only if you select a different name from the default
    public $settingsFieldName = 'user_settings';
}
```

#### Option 2 - `HasSettingsTable` trait

[](#option-2---hassettingstable-trait)

Run before the command `php artisan model-settings:model-settings-table`.
The command will copy for you the migration class to create the table where the setting values will be stored.
The default name of the table is `model_settings`; change the config or env value `MODEL_SETTINGS_TABLE_NAME` if you want to rewrite the default name (**before you run the command!**)

```
use Lukasss93\ModelSettings\Traits\HasSettingsTable;

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

#### Option 3 - `HasSettingsRedis` trait

[](#option-3---hassettingsredis-trait)

```
use Lukasss93\ModelSettings\Traits\HasSettingsRedis;

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

👓 Usage
-------

[](#-usage)

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

#### Check id the settings for the entity is empty

[](#check-id-the-settings-for-the-entity-is-empty)

```
$user->settings()->empty();
```

#### Check settings (exist)

[](#check-settings-exist)

```
$user->settings()->exist();
```

#### Get all model's settings

[](#get-all-models-settings)

```
$user->settings()->all();
$user->settings()->get();
```

#### Get a specific setting

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

```
$user->settings()->get('some.setting');
$user->settings()->get('some.setting', 'default value');
//multiple
$user->settings()->getMultiple(
	[
		'some.setting_1',
		'some.setting_2',
	],
	'default value'
);
```

#### Add / Update setting

[](#add--update-setting)

```
$user->settings()->apply((array)$settings);
$user->settings()->set('some.setting', 'new value');
$user->settings()->update('some.setting', 'new value');
//multiple
$user->settings()->setMultiple([
	'some.setting_1' => 'new value 1',
	'some.setting_2' => 'new value 2',
]);
```

#### Check if the model has a specific setting

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

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

#### Remove a setting from a model

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

```
$user->settings()->delete('some.setting');
//multiple
$user->settings()->deleteMultiple([
	'some.setting_1',
	'some.setting_2',
]);
//all
$user->settings()->clear();
```

### ⬇ Persistence for settings field

[](#-persistence-for-settings-field)

In case of field settings the auto-save is configurable.

**The `default` value is `true`**

- Use an attribute on model

```
protected $persistSettings = true; //boolean
```

- Environment (.env) variable

```
MODEL_SETTINGS_PERSISTENT=true
```

- Config value - model settings config file

```
'settings_persistent' => env('MODEL_SETTINGS_PERSISTENT', true),
```

If the persistence is `false` you have to save the model after the operation.

### 🛠️ Using another method name other than `settings()`

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

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

### 🌠 Default settings

[](#-default-settings)

You can set default configs for a model:

```
use Lukasss93\ModelSettings\Traits\HasSettingsTable;

class User extends Model
{
    use HasSettingsTable;

    public function defaultSettings(): array
    {
        return [
            'foo' => 'bar',
        ];
    }
}
```

### 🎉 Initializing settings at model creation

[](#-initializing-settings-at-model-creation)

You can initialize settings at model creation by defining a `initSettings` property in your model:

```
use Lukasss93\ModelSettings\Traits\HasSettingsTable;

class User extends Model
{
    use HasSettingsTable;

    public bool $initSettings = true;

    public function defaultSettings(): array
    {
        return [
            'foo' => 'bar',
        ];
    }
}
```

### 🔍 Validation system for settings data

[](#-validation-system-for-settings-data)

When you're using the `set()`, `apply()`, `update()` methods thrown an exception when you break a rule.
You can define rules on model using `settingsRules` public method, and the rules array definition is identical with the Laravel default [validation rules](https://laravel.com/docs/9.x/validation#available-validation-rules).

```
class User extends Model
{
    use HasSettingsTable;

    public function defaultSettings(): array
    {
        return [
            'info' => [
                'email' => 'user@test.com'
                'age' => 27,
            ],
            'language' => 'en',
            'max_size' => 12,
        ];
    }

    public function settingsRules(): array
    {
        return [
            'info' => 'array',
            'info.email' => ['string','email'],
            'info.age' => 'integer',
            'language' => 'string|in:en,es,it|max:2',
            'max_size' => 'int|min:5|max:15',
        ];
    }
}
```

📃 Changelog
-----------

[](#-changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

📖 License
---------

[](#-license)

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

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 61.1% 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 ~168 days

Total

4

Last Release

788d ago

### Community

Maintainers

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

---

Top Contributors

[![glorand](https://avatars.githubusercontent.com/u/883989?v=4)](https://github.com/glorand "glorand (55 commits)")[![Lukasss93](https://avatars.githubusercontent.com/u/4071613?v=4)](https://github.com/Lukasss93 "Lukasss93 (30 commits)")[![chadidi](https://avatars.githubusercontent.com/u/9916806?v=4)](https://github.com/chadidi "chadidi (1 commits)")[![niveshsaharan](https://avatars.githubusercontent.com/u/2552126?v=4)](https://github.com/niveshsaharan "niveshsaharan (1 commits)")[![nmfzone](https://avatars.githubusercontent.com/u/10361906?v=4)](https://github.com/nmfzone "nmfzone (1 commits)")[![james2doyle](https://avatars.githubusercontent.com/u/1425304?v=4)](https://github.com/james2doyle "james2doyle (1 commits)")[![FoksVHox](https://avatars.githubusercontent.com/u/37844753?v=4)](https://github.com/FoksVHox "FoksVHox (1 commits)")

---

Tags

laravelSettingslaravel-settingslaravel-model-settings

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/lukasss93-laravel-model-settings/health.svg)](https://phpackages.com/packages/lukasss93-laravel-model-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)[aedart/athenaeum

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

245.2k](/packages/aedart-athenaeum)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[glorand/laravel-model-settings

Model Settings for your Laravel app

9041.1M4](/packages/glorand-laravel-model-settings)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)

PHPackages © 2026

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