{"id":13828215,"url":"https://github.com/glorand/laravel-model-settings","last_synced_at":"2026-01-11T16:51:13.826Z","repository":{"id":33971785,"uuid":"163381474","full_name":"glorand/laravel-model-settings","owner":"glorand","description":"Model Settings for your Laravel app","archived":false,"fork":false,"pushed_at":"2024-07-18T17:49:32.000Z","size":113,"stargazers_count":781,"open_issues_count":3,"forks_count":55,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-09-08T16:43:55.512Z","etag":null,"topics":["eloquent-models","laravel","laravel-settings","settings","trait"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/glorand.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-28T07:37:42.000Z","updated_at":"2024-09-08T01:54:12.000Z","dependencies_parsed_at":"2024-06-18T12:34:51.913Z","dependency_job_id":"bb2fca25-3023-4aad-bde9-91354c698393","html_url":"https://github.com/glorand/laravel-model-settings","commit_stats":{"total_commits":62,"total_committers":12,"mean_commits":5.166666666666667,"dds":"0.17741935483870963","last_synced_commit":"aa7b232856daa8379907f17f37db76a1021a3695"},"previous_names":[],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glorand%2Flaravel-model-settings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glorand%2Flaravel-model-settings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glorand%2Flaravel-model-settings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glorand%2Flaravel-model-settings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/glorand","download_url":"https://codeload.github.com/glorand/laravel-model-settings/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225486571,"owners_count":17481930,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent-models","laravel","laravel-settings","settings","trait"],"created_at":"2024-08-04T09:02:36.872Z","updated_at":"2026-01-11T16:51:13.816Z","avatar_url":"https://github.com/glorand.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003cimg height=\"100px\" alt=\"laravel\" src=\"https://user-images.githubusercontent.com/883989/60343130-f5021800-99bb-11e9-8a03-fe11746a86c2.png\"\u003e\n\u003c/p\u003e\n\n\u003ch6 align=\"center\"\u003e\n    Model Settings for your Laravel app\n\u003c/h6\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://packagist.org/packages/glorand/laravel-model-settings/stats\"\u003e\n\u003cimg src=\"https://img.shields.io/packagist/dt/glorand/laravel-model-settings?style=for-the-badge\u0026color=red\" alt=\"Total Downloads\"/\u003e\n\u003c/a\u003e\n\u003cbr /\u003e\n\u003ca href=\"https://packagist.org/packages/glorand/laravel-model-settings\"\u003e\n \u003cimg src=\"https://img.shields.io/packagist/v/glorand/laravel-model-settings\" alt=\"Latest Stable Version\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/glorand/laravel-model-settings/actions\"\u003e\n\u003cimg src=\"https://github.com/glorand/laravel-model-settings/workflows/Test/badge.svg\" alt=\"'Github Actions\" /\u003e\n\u003c/a\u003e\n \u003ca href=\"LICENSE\"\u003e\n \u003cimg src=\"https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat\" alt=\"Software License\"\u003e\n \u003c/a\u003e\n \u003cbr /\u003e\n \u003ca title=\"MadeWithLaravel.com Shield\" href=\"https://madewithlaravel.com/p/laravel-model-settings/shield-link\"\u003e \u003cimg src=\"https://madewithlaravel.com/storage/repo-shields/1716-shield.svg\" alt=\"Sheald\"/\u003e\u003c/a\u003e\n\u003ca title=\"PHP Version\" href=\"#\"\u003e\u003cimg src=\"https://img.shields.io/packagist/php-v/glorand/laravel-model-settings\" alt=\"PHP Version\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nThe package requires PHP 8.1+ and follows the FIG standards PSR-1, PSR-2, PSR-4 and PSR-12\nto ensure a high level of interoperability between shared PHP.\n\nBug reports, feature requests, and pull requests can be submitted by following our [Contribution Guide](CONTRIBUTING.md).\n\n## Table of contents\n- [Installation](#installation)\n- [Updating your Eloquent Models](#update_models)\n    - [Option 1 - `HasSettingsField` trait](#update_models_1)\n    - [Option 2 - `HasSettingsTable` trait](#update_models_2)\n    - [Option 3 - `HasSettingsRedis` trait](#update_models_3)\n- [Default Settings](#default_settings)\n- [Usage](#usage)\n    - [Check if the settings for the entity is empty (exist)](#empty)\n    - [Check settings (exist)](#exist)\n    - [Get all model's settings](#get_all)\n    - [Get a specific setting](#get)\n    - [Add / Update setting](#add_update)\n    - [Check if the model has a specific setting](#check)\n    - [Remove a setting from a model](#remove)\n    - [Persistence](#persistence)\n    - [Using another method name other than `settings()`](#invokeSettingsBy)\n    - [Validation system for settings data](#validation)\n - [Changelog](#changelog)\n - [Contributing](#contributing)\n- [License](#license)\n\n## Installation \u003ca name=\"installation\"\u003e\u003c/a\u003e\n```shell\n$ composer require glorand/laravel-model-settings\n```\n\n```\n{\n    \"require\": {\n        \"glorand/laravel-model-settings\": \"^4.0\"\n    }\n}\n```\n\n## Env (config) variables **(.env file)**\n\nDefault name for the settings field - when you use the `HasSettingsField`\n\n`MODEL_SETTINGS_FIELD_NAME=settings`\n\nDefault name for the settings table - when you use the `HasSettingsTable`\n\n`MODEL_SETTINGS_TABLE_NAME=model_settings`\n\n## Updating your Eloquent Models \u003ca name=\"update_models\"\u003e\u003c/a\u003e\nYour models should use the `HasSettingsField` or `HasSettingsTable` trait.\n\n#### Option 1 - `HasSettingsField` trait \u003ca name=\"update_models_1\"\u003e\u003c/a\u003e\nRun the `php artisan model-settings:model-settings-field` in order to create a migration file for a table.\\\nThis command will create a json field (default name `settings`, from config) for the mentioned table.\n\nYou can choose another than default, in this case you have to specify it in you model.\n```php\npublic $settingsFieldName = 'user_settings';\n```\n\nComplete example:\n```php\nuse Glorand\\Model\\Settings\\Traits\\HasSettingsField;\n\nclass User extends Model\n{\n    use HasSettingsField;\n\n    //define only if you select a different name from the default\n    public $settingsFieldName = 'user_settings';\n\n    //define only if the model overrides the default connection\n    protected $connection = 'mysql';\n\n}\n```\n#### Option 2 - `HasSettingsTable` trait \u003ca name=\"update_models_2\"\u003e\u003c/a\u003e\nRun before the command `php artisan model-settings:model-settings-table`.\\\nThe command will copy for you the migration class to create the table where the setting values will be stored.\\\nThe 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!**)\n```php\nuse Glorand\\Model\\Settings\\Traits\\HasSettingsTable;\n\nclass User extends Model\n{\n    use HasSettingsTable;\n}\n```\n\n#### Option 3 - `HasSettingsRedis` trait \u003ca name=\"update_models_3\"\u003e\u003c/a\u003e\n```php\nuse Glorand\\Model\\Settings\\Traits\\HasSettingsRedis;\n\nclass User extends Model\n{\n    use HasSettingsRedis;\n}\n```\n\n## Default settings \u003ca name=\"default_settings\"\u003e\u003c/a\u003e\n\nYou can set default configs for a table in model_settings.php config file\n\n```php\nreturn [\n    // start other config options\n\n    // end other config options\n\n    // defaultConfigs\n    'defaultSettings' =\u003e [\n        'users' =\u003e [\n            'key_1' =\u003e 'val_1',\n        ]\n    ]\n];\n```\n\nOr in your model itself:\n\n```php\nuse Glorand\\Model\\Settings\\Traits\\HasSettingsTable;\n\nclass User extends Model\n{\n    public $defaultSettings = [\n        'key_1' =\u003e 'val_1',\n    ];\n}\n```\n\n\u003e Please note that if you define settings in the model, the settings from configs will have no effect, they will just be ignored.\n\n\n## Usage \u003ca name=\"usage\"\u003e\u003c/a\u003e\n\n```php\n$user = App\\User::first();\n```\n\n#### Check if the settings for the entity is empty \u003ca name=\"empty\"\u003e\u003c/a\u003e\n```php\n$user-\u003esettings()-\u003eempty();\n```\n\n#### Check settings (exist) \u003ca name=\"exist\"\u003e\u003c/a\u003e\n```php\n$user-\u003esettings()-\u003eexist();\n```\n\n#### Get all model's settings \u003ca name=\"get_all\"\u003e\u003c/a\u003e\n```php\n$user-\u003esettings()-\u003eall();\n$user-\u003esettings()-\u003eget();\n```\n\n#### Get a specific setting \u003ca name=\"get\"\u003e\u003c/a\u003e\n```php\n$user-\u003esettings()-\u003eget('some.setting');\n$user-\u003esettings()-\u003eget('some.setting', 'default value');\n//multiple\n$user-\u003esettings()-\u003egetMultiple(\n\t[\n\t\t'some.setting_1',\n\t\t'some.setting_2',\n\t],\n\t'default value'\n);\n```\n\n#### Add / Update setting \u003ca name=\"add_update\"\u003e\u003c/a\u003e\n```php\n$user-\u003esettings()-\u003eapply((array)$settings);\n$user-\u003esettings()-\u003eset('some.setting', 'new value');\n$user-\u003esettings()-\u003eupdate('some.setting', 'new value');\n//multiple\n$user-\u003esettings()-\u003esetMultiple([\n\t'some.setting_1' =\u003e 'new value 1',\n\t'some.setting_2' =\u003e 'new value 2',\n]);\n```\n\n#### Check if the model has a specific setting \u003ca name=\"check\"\u003e\u003c/a\u003e\n```php\n$user-\u003esettings()-\u003ehas('some.setting');\n```\n\n#### Remove a setting from a model \u003ca name=\"remove\"\u003e\u003c/a\u003e\n```php\n$user-\u003esettings()-\u003edelete('some.setting');\n//multiple\n$user-\u003esettings()-\u003edeleteMultiple([\n\t'some.setting_1',\n\t'some.setting_2',\n]);\n//all\n$user-\u003esettings()-\u003eclear();\n```\n\n#### Persistence for settings field \u003ca name=\"persistence\"\u003e\u003c/a\u003e\nIn case of field settings the auto-save is configurable.\n\n**The ``default`` value is ``true``**\n\n - Use an attribute on model\n```php\nprotected $persistSettings = true; //boolean\n```\n - Environment (.env) variable\n ```dotenv\nMODEL_SETTINGS_PERSISTENT=true\n```\n- Config value - model settings config file\n ```php\n'settings_persistent' =\u003e env('MODEL_SETTINGS_PERSISTENT', true),\n```\nIf the persistence is `false` you have to save the model after the operation.\n\n### Using another method name other than `settings()` \u003ca name=\"invokeSettingsBy\"\u003e\u003c/a\u003e\nIf you prefer to use another name other than `settings` ,\nyou can do so by defining a `$invokeSettingsBy` property. \nThis forward calls (such as `configurations()`) to the `settings()` method.\n\n### Validation system for settings data \u003ca name=\"validation\"\u003e\u003c/a\u003e\nWhen you're using the set() or apply()|update() methods thrown an exception when you break a rule.\nYou can define rules on model using `$settingsRules` public property, and the rules array definition is identical with\nthe Laravel default validation rules. ([see Laravel rules](https://laravel.com/docs/8.x/validation#available-validation-rules))\n```php\nclass User extends Model\n{\n    use HasSettingsTable;\n\n    public array $defaultSettings = [\n        'user' =\u003e [\n            'name' =\u003e 'Test User',\n            'email' =\u003e 'user@test.com'\n            'age' =\u003e 27,\n        ],\n        'language' =\u003e 'en',\n        'max_size' =\u003e 12,\n    ];\n\n    // settings rules\n    public array $settingsRules = [\n        'user' =\u003e 'array',\n        'user.email' =\u003e [\n            'string',\n            'email',\n        ],\n        'user.age' =\u003e 'integer',\n        'language' =\u003e 'string|in:en,es,it|max:2',\n        'max_size' =\u003e 'int|min:5|max:15',\n    ];\n}\n\n```\n\n## Changelog \u003ca name=\"changelog\"\u003e\u003c/a\u003e\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing \u003ca name=\"contributing\"\u003e\u003c/a\u003e\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## License \u003ca name=\"license\"\u003e\u003c/a\u003e\nThe MIT License (MIT). Please see [LICENSE](LICENSE) for more information.\n\n## Related Stuff\n- [LaraNews - Laravel Model Settings](https://laravel-news.com/laravel-model-settings)\n- [made with Laravel - Laravel Model Settings](https://madewithlaravel.com/laravel-model-settings)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglorand%2Flaravel-model-settings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fglorand%2Flaravel-model-settings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglorand%2Flaravel-model-settings/lists"}