Package Data | |
---|---|
Maintainer Username: | glorand |
Maintainer Contact: | gombos.lorand@gmail.com (Lorand Gombos) |
Package Create Date: | 2018-12-28 |
Package Last Update: | 2024-07-18 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-30 15:21:02 |
Package Statistics | |
---|---|
Total Downloads: | 738,791 |
Monthly Downloads: | 15,389 |
Daily Downloads: | 221 |
Total Stars: | 819 |
Total Watchers: | 12 |
Total Forks: | 56 |
Total Open Issues: | 4 |
The package requires PHP 7.1.3+ and follows the FIG standards PSR-1, PSR-2 and PSR-4 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.
$ composer require glorand/laravel-model-settings
{
"require": {
"glorand/laravel-model-settings": "^3.0"
}
}
Default name for the settings field - when you use the HasSettingsField
MODEL_SETTINGS_FIELD_NAME=settings
Default name for the settings table - when you use the HasSettingsTable
MODEL_SETTINGS_TABLE_NAME=model_settings
Your models should use the HasSettingsField
or HasSettingsTable
trait.
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.
You can choose another than default, in this case you have to specify it in you model.
public $settingsFieldName = 'user_settings';
Complete example:
use Glorand\Model\Settings\Traits\HasSettingsField;
class User extends Model
{
use HasSettingsField;
//define only if you select a different name from the default
public $settingsFieldName = 'user_settings';
}
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 Glorand\Model\Settings\Traits\HasSettingsTable;
class User extends Model
{
use HasSettingsTable;
}
$user = App\User::first();
$user->settings()->all();
$user->settings()->get();
$user->settings()->get('some.setting');
$user->settings()->get('some.setting', 'default value');
$user->settings()->apply((array)$settings);
$user->settings()->set('some.setting', 'new value');
$user->settings()->update('some.setting', 'new value');
$user->settings()->has('some.setting');
$user->settings()->delete('some.setting');
In case of field settings the auto-save is configurable
protected $persistSettings = true; //boolean
MODEL_SETTINGS_PERSISTENT=true
'settings_persistent' => env('MODEL_SETTINGS_PERSISTENT', false),
If the persistence is false
you have to save the model after the operation.
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see LICENSE for more information.