r94ever / laravel-model-setting
Add setting function to Laravel models.
Requires
- php: ^7.1|^8.0
- ext-json: *
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0|^6.0
- phpunit/phpunit: ^8.0|^9.0
This package is auto-updated.
Last update: 2024-10-26 13:22:42 UTC
README
Add setting function for Laravel Models.
Installation
You can install the package via composer:
composer require r94ever/laravel-model-setting
Publish config and migration
php artisan vendor:publish --provider="r94ever\LaravelModelSetting\ModelSettingServiceProvider"
Open config/model-setting.php
file then set the name of table which hold the setting data to your desire.
Run migration
php artisan migrate
Usage
Implement HasSettingsContract
then add HasSettingData
trait to your model
use Illuminate\Database\Eloquent\Model; use r94ever\LaravelModelSetting\Interfaces\HasSettingContract; use r94ever\LaravelModelSetting\Traits\HasSettingTrait; class Post extends Model implements HasSettingContract { use HasSettingTrait; ... }
Save settings:
$model->saveSetting(array $settings);
Check whether model has setting:
$model->hasSetting(string $key); // return true or false
Check whether model has multiple given settings:
$model->hasSettings(array $keys, bool $any = false); // return true or false // When $any is FALSE, method will return TRUE only if model has all given setting keys // Otherwise, method will return TRUE if model has any of the given setting keys
Get value of the given setting key
$model->getSetting(string $key, $defaultValue);
Or get values from multiple setting keys
$model->getSettings(array $keys);
Query to find model which has given meta key & meta value
$post = Post::whereSetting('setting_key', $settingValue)->get();
For nested keys:
$post = Post::whereSetting('key->sub_key->sub_sub_key', $settingValue)->get(); $post = Post::whereSetting('key.sub_key.sub_sub_key', $settingValue)->get();
If using MySQL or PostgreSQL, you can use array for $settingValue
$post = Post::whereSetting('key->sub_key->sub_sub_key', ['value_1', 'value_2'])->get(); $post = Post::whereSetting('key.sub_key.sub_sub_key', ['value_1', 'value_2'])->get();
Changelog
Please see CHANGELOG for more information what has changed recently.
Security
If you discover any security related issues, please email vandt147@outlook.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.