Allows eloquent models to have its own settings
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
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 [];
}
}
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);
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);
Get all settings. It will return array of settings
$settings = $user->getSettings(); // ['timezone' => 'UTC', 'lang' => 'en_us', 'subscription' => ['newsletter' => true]];