New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

maikelvanmaurik/laravel-model-settings

Add simple yet flexible settings to your Laravel models.
1,694
Install
composer require maikelvanmaurik/laravel-model-settings
Latest Version:1.3.2
PHP:>=5.5.9
Laravel:5 · 6 · 7
License:MIT
Last Updated:Apr 2, 2020
Links: GitHub  ·  Packagist
Maintainer: maikelvanmaurik

laravel-model-settings

Simple yet flexible settings for your Laravel models.

Note: I will be updating this plugin in the near future to better match the API of the new cache() helper method that has been introduced in Laravel 5.3

Installation

1.) Install via composer
composer require cklmercer/laravel-model-settings
2.) Add a JSON settings field to your model's 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');
    $table->rememberToken();
    $table->timestamps();
});
3.) Use the trait Cklmercer\ModelSettings\HasSettings within your model.

User.php

use Cklmercer\ModelSettings\HasSettings;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable 
{
    use HasSettings;
     
    // truncated for brevity..
}

Usage

1.) Get all of the model's settings.
$user = App\User::first();

$user->settings()->all(); // Returns an array of the user's settings.
$user->settings()->get(); // Returns an array of the user's settings.
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('some.setting'); // Quicker access.
3.) Add or update a setting.
$user = App\User::first();

$user->settings()->set('some.setting', 'new value');
$user->settings()->update('some.setting', 'new value');
4.) Determine if the model has a specific setting.
$user = App\User::first();

$user->settings()->has('some.setting');
5.) Remove a setting from a model.
$user = App\User::first();

$user->settings()->delete('some.setting');
$user->settings()->forget('some.setting');
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 Cklmercer\ModelSettings\HasSettings;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable 
{
    use HasSettings;

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

    // truncated for brevity..
}
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 Cklmercer\ModelSettings\HasSettings;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable 
{
    use HasSettings;

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

    // truncated for brevity..
}
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 Cklmercer\ModelSettings\HasSettings;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable 
{
    use HasSettings;

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

    // truncated for brevity..
}

License

MIT

Related Packages

sfneal/models

Eloquent Model wrapper with extended functionality

98,299 1
npabisz/laravel-settings

Settings for Laravel.

8,789 27
merodiro/settings

Easy global/user settings

2,713 35
smatar/laravel-settings

Simple Settings package for laravel application

2,405 0
webmachine/settings

Settings for Laravel 5

396 0

Version History

Version Released PHP Laravel License
1.3.2 >=5.5.9 ^5.2||^6.0||^7.0 MIT
1.3.1 >=5.5.9 ^5.2||^6.0 MIT
1.3.0 >=5.5.9 ^5.2|^6.0 MIT
v1.2.0 >=5.5.9 ^5.2 MIT
v1.1.1 >=5.5.9 MIT