My Blog Posts

Laravel Model Settings

Laravel Model Settings


The package has two properties (one or the other, not both) that you can configure in a specific model (settings in one field or table). First, the settings field is a JSON field:

// Settings field

use Glorand\Model\Settings\Traits\HasSettingsField;

class User extends Model
{
    use HasSettingsField;

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

}

And a separate setting table option:

use Glorand\Model\Settings\Traits\HasSettingsTable;

class User extends Model
{
    use HasSettingsTable;
}

Here are some basic examples of basic uses in a model:

Get all settings
$user->settings()->all();

Get a specific setting
$user->settings()->get('some.setting');
$user->settings()->get('some.setting', 'default value');

Add/Update
$user->settings()->apply((array)$settings);
$user->settings()->set('some.setting', 'new value');
$user->settings()-update('some.setting', 'new value');

Remove
$user->settings()->delete('some.setting');

You can learn more about this package, get all the setup instructions, and view the source code at glorand/laravel-model-settings on GitHub.



Share this article


Comments (0)

Comment