supplycart / settings
Model settings for Laravel
Installs: 42 334
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: ^8.3
Requires (Dev)
- orchestra/canvas: ^9.0
- orchestra/testbench: ^9.0
This package is auto-updated.
Last update: 2025-03-18 09:13:48 UTC
README
Allows eloquent models to have its own settings
Installation
To install, run this on your Laravel installation:
composer require supplycart/settings
Then publish the migration file:
php artisan vendor:publish --tag=migrations --provider=Supplycart\Settings\Providers\SettingsServiceProvider
Usage
To use, you just need to implement the Supplycart\Settings\Contracts\HasSettings
contract and use Supplycart\Settings\Traits\HasSettings
trait:
use Supplycart\Settings\Contracts\HasSettings as HasSettingsContract; use Supplycart\Settings\Traits\HasSettings; class User extends Model implements HasSettingsContract { use HasSettings; public function getDefaultSettings(): array { return []; } }
Methods
getSetting($key, $default = null)
Retrieve model setting by key. You can use dot notations to get nested setting e.g
$user->getSetting('timezone', 'Asia/Kuala_Lumpur'); $user->getSetting('lang', 'en_my'); $user->getSetting('subscription.newsletter', false);
setSetting($key, $value)
Set model setting using key. You can use dot notation same like getSetting
method e.g
$user->setSetting('timezone', 'UTC'); $user->setSetting('lang', 'en_us'); $user->setSetting('subscription.newsletter', true);
getSettings()
Get all settings. It will return array of settings
$settings = $user->getSettings(); // ['timezone' => 'UTC', 'lang' => 'en_us', 'subscription' => ['newsletter' => true]];