rolfhaug / laravel-model-settings
A package that gives you a fluent api to add key/value settings to any Eloquent model
Installs: 1 778
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 2
Open Issues: 1
Type:package
Requires (Dev)
- fzaninotto/faker: ^1.8@dev
- laravel/laravel: 5.*
- nunomaduro/collision: ^2.0
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-11-11 01:04:50 UTC
README
This package allows you to easily extend any Laravel Model, with a Settings Model. Perfect for configuration settings and personal preferences.
You can then easily set and retrieve key/value pairs through an elegant API. If the setting does not exist, NULL
will be returned.
$user = User::find(1);
$user->settings->set('my_setting', 'my_value');
echo $user->settings->my_setting; // my_value
Installation
composer require rolfhaug/laravel-model-settings
If you're using an older version than Laravel 5.6, add this to your providers in config/app.php
.
RolfHaug\ModelSettings\ModelSettingsProvider::class,
Create a new Settings model
- Use the command to create a new settings model
art make:model-settings --model=user
- Add the Settings trait to your model
use RolfHaug\ModelSettings\Settings;
Tip: The command creates a migration that can be rolled back.
How to use the package
$user = User:find(1);
Set settings
// Single setting
$user->settings->set('newsletter', true);
// Array of settings
$settings = [
['awesome_setting' => 'awesome_value'],
['another_setting' => 'another_value']
];
$user->settings->set($settings);
Access settings
If the setting does not exist, NULL
will be returned.
$user->settings->newsletter;
// Array of all available settings
$user->settings->all();
Destroy setting
$user->settings->delete('newsletter');
Scopes
$users = User::whereSetting('newsletter', true)->get();
$users = User::whereHasSetting('newsletter')->get();
$users = User::whereDoesntHaveSetting('newsletter')->get();