Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/supplycart/settings
Laravel model settings
https://github.com/supplycart/settings
hacktoberfest laravel settings
Last synced: 26 days ago
JSON representation
Laravel model settings
- Host: GitHub
- URL: https://github.com/supplycart/settings
- Owner: supplycart
- Created: 2019-12-20T04:15:04.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-18T07:42:32.000Z (6 months ago)
- Last Synced: 2024-10-27T21:20:57.785Z (about 2 months ago)
- Topics: hacktoberfest, laravel, settings
- Language: PHP
- Homepage:
- Size: 43.9 KB
- Stars: 0
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Settings
Allows eloquent models to have its own settings
## Installation
To install, run this on your Laravel installation:
```shell
composer require supplycart/settings
```Then publish the migration file:
```shell
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:
```php
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
```php
$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
```php
$user->setSetting('timezone', 'UTC');
$user->setSetting('lang', 'en_us');
$user->setSetting('subscription.newsletter', true);
```#### getSettings()
Get all settings. It will return array of settings
```php
$settings = $user->getSettings(); // ['timezone' => 'UTC', 'lang' => 'en_us', 'subscription' => ['newsletter' => true]];
```