|
| 1 | +# Laravel Settings |
| 2 | + |
| 3 | +Introducing "Laravel Settings" – a powerful and flexible Laravel package designed to simplify the management of application settings, both at the global and model-specific levels. With this package, developers can effortlessly store, retrieve, and customize settings within the database, enhancing the configuration and flexibility of Laravel-based projects. |
| 4 | + |
| 5 | +Whether you need to manage site-wide configurations, user preferences, or any other form of customizable settings, "Laravel Settings" provides an elegant solution to streamline the process. It offers a clean and intuitive API, ensuring that you can get started quickly without any steep learning curve. |
| 6 | + |
| 7 | +## Key Features: |
| 8 | + |
| 9 | +- Global and Model-Specific Settings: "Laravel Settings" allows you to define both global settings, which apply to your entire application, and model-specific settings, which are associated with specific Eloquent models. |
| 10 | + |
| 11 | +- Easy Configuration: You can define settings in a configuration file, making it a breeze to set up and manage your application's settings. |
| 12 | + |
| 13 | +- Customizable: The package offers the flexibility to create custom setting types, ensuring that you can handle various data types, validation rules, and display options. |
| 14 | + |
| 15 | +- Eloquent Integration: "Laravel Settings" seamlessly integrates with Laravel's Eloquent ORM, enabling you to link settings to specific database records. |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +- Laravel ^10.0 |
| 20 | + |
| 21 | +## Installation: |
| 22 | + |
| 23 | +To get started with "Laravel Settings," follow these simple installation steps: |
| 24 | + |
| 25 | +Install the package via Composer: |
| 26 | + |
| 27 | +```bash |
| 28 | +composer require ruangdeveloper/laravel-settings |
| 29 | +``` |
| 30 | + |
| 31 | +**Publish the configuration file:** |
| 32 | + |
| 33 | +```bash |
| 34 | +php artisan vendor:publish --provider="RuangDeveloper\LaravelSettings\LaravelSettingsServiceProvider" --tag="config" |
| 35 | +``` |
| 36 | + |
| 37 | +Configure the settings in `config/laravel-settings.php` according to your application's requirements. |
| 38 | + |
| 39 | +**Publish the migration file** |
| 40 | + |
| 41 | +```bash |
| 42 | +php artisan vendor:publish --provider="RuangDeveloper\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +Let's take a quick look at how you can use "Laravel Settings" to manage a global setting for your application. |
| 48 | + |
| 49 | +**Set a global setting** |
| 50 | + |
| 51 | +```php |
| 52 | +use RuangDeveloper\LaravelSettings\Facades\Settings; |
| 53 | + |
| 54 | +// setting the global site title |
| 55 | +Settings::set('site_title', 'Your Awesome Website'); |
| 56 | + |
| 57 | +``` |
| 58 | + |
| 59 | +**Get a global setting** |
| 60 | + |
| 61 | +```php |
| 62 | +use RuangDeveloper\LaravelSettings\Facades\Settings; |
| 63 | + |
| 64 | +// retrieve the global site title |
| 65 | +$siteTItle = Settings::get('site_title'); |
| 66 | + |
| 67 | +// you may want to add a default fallback value if the setting |
| 68 | +// with provided key doesn't exists in the database |
| 69 | +$siteTitle = Settings::get('site_title', 'Your Default Awesome Website'); |
| 70 | +``` |
| 71 | + |
| 72 | +### Forget a global setting |
| 73 | + |
| 74 | +Now, if you want to delete the setting |
| 75 | + |
| 76 | +```php |
| 77 | +use RuangDeveloper\LaravelSettings\Facades\Settings; |
| 78 | + |
| 79 | +Settings::forget('site_title'); |
| 80 | +``` |
| 81 | + |
| 82 | +## Model Specific Setting |
| 83 | + |
| 84 | +This package allow you to link the setting to a specific model. For example, you may want to store user's preferences. |
| 85 | + |
| 86 | +**Model Configuration** |
| 87 | +The first step before linking the setting to a specific model, you need to configure your model to use the `HasSettings` traits. |
| 88 | + |
| 89 | +```php |
| 90 | +use Illuminate\Database\Eloquent\Model; |
| 91 | +use RuangDeveloper\LaravelSettings\Traits\HasSettings; |
| 92 | + |
| 93 | +class User extends Model |
| 94 | +{ |
| 95 | + use HasSettings; // use the HasSettings trait |
| 96 | + |
| 97 | + protected $guarded = []; |
| 98 | +} |
| 99 | + |
| 100 | +``` |
| 101 | + |
| 102 | +Now, you can use the setting for an user. |
| 103 | + |
| 104 | +**Set a setting** |
| 105 | + |
| 106 | +```php |
| 107 | +$user = App\Models\User::find(1); |
| 108 | +$user->setSetting('subscribe_newsletter', true); |
| 109 | +``` |
| 110 | + |
| 111 | +**Get a setting** |
| 112 | + |
| 113 | +```php |
| 114 | +$user = App\Models\User::find(1); |
| 115 | +$isSubscribed = $user->getSetting('subscribe_newsletter'); |
| 116 | + |
| 117 | +// you may want to add a default fallback value if the setting |
| 118 | +// with provided key doesn't exists in the database |
| 119 | +$isSubscribed = $user->getSetting('subscribe_newsletter', false); |
| 120 | +// |
| 121 | +``` |
| 122 | + |
| 123 | +**Forget a setting** |
| 124 | + |
| 125 | +```php |
| 126 | +$user = App\Models\User::find(1); |
| 127 | +$user->forgetSetting('subscribe_newsletter'); |
| 128 | +``` |
| 129 | + |
| 130 | +## Default Settings |
| 131 | + |
| 132 | +You may want add default settings value for global or model specific settings. You can add these settings value in the configuration file located at `configs/laravel-settings.php`. |
| 133 | + |
| 134 | +> Note: if you cannot find the config file, you need to publish the configuration first. |
| 135 | +
|
| 136 | +**Default global settings** |
| 137 | + |
| 138 | +```php |
| 139 | +return [ |
| 140 | + // others config |
| 141 | + |
| 142 | + 'defaults' => [ |
| 143 | + 'site_title' => 'Your Awesome Site', |
| 144 | + ], |
| 145 | +]; |
| 146 | +``` |
| 147 | + |
| 148 | +Now, you will always get the default site title setting if there is no setting stored in the database. |
| 149 | + |
| 150 | +```php |
| 151 | +use RuangDeveloper\LaravelSettings\Facades\Settings; |
| 152 | + |
| 153 | + |
| 154 | +$siteTItle = Settings::get('site_title'); |
| 155 | + |
| 156 | +echo $siteTitle; // Your Awesome Site |
| 157 | +``` |
| 158 | + |
| 159 | +**Default model specific settings** |
| 160 | + |
| 161 | +```php |
| 162 | +return [ |
| 163 | + // others config |
| 164 | + |
| 165 | + 'model_defaults' => [ |
| 166 | + 'App\Models\User' => [ |
| 167 | + 'subscribe_newsletter' => false |
| 168 | + ], |
| 169 | + ], |
| 170 | +]; |
| 171 | +``` |
| 172 | + |
| 173 | +Now, you will always get the default subscribe newsletter setting when you try to retrieve it from an user that didn't have setting for subscribe newsletter. |
| 174 | + |
| 175 | +```php |
| 176 | +$user = App\Models\User::find(1); |
| 177 | +$isSubscribed = $user->getSetting('subscribe_newsletter'); |
| 178 | + |
| 179 | +echo $isSubscribed // false |
| 180 | +``` |
| 181 | + |
| 182 | +## Customization |
| 183 | + |
| 184 | +### Using Your Own Model |
| 185 | + |
| 186 | +While this package covers almost everything that you need to manage the settings, you may want to use your own model. To do this, you can extends the Setting model from this package. |
| 187 | + |
| 188 | +```php |
| 189 | +use RuangDeveloper\LaravelSettings\Models\Setting; |
| 190 | + |
| 191 | +class CustomSetting extends Setting |
| 192 | +{ |
| 193 | + // |
| 194 | +} |
| 195 | +``` |
| 196 | + |
| 197 | +After that, you need to tell this package to use the model you just created. Please update in the `configs/laravel-settings.php`. |
| 198 | + |
| 199 | +```php |
| 200 | +return [ |
| 201 | + // others config |
| 202 | + |
| 203 | + 'model' => App\Models\CustomSetting::class, |
| 204 | +]; |
| 205 | +``` |
| 206 | + |
| 207 | +Certainly, adding a "Contributing" section to your package's documentation is a great way to invite other developers to collaborate and make your package even better. Here's a sample "Contributing" section that you can include: |
| 208 | + |
| 209 | +# Contributing |
| 210 | + |
| 211 | +https://github.com/ruangdeveloper/laravel-settings/CONTRIBUTING.md |
| 212 | + |
| 213 | +# License |
| 214 | + |
| 215 | +https://github.com/ruangdeveloper/laravel-settings/LICENSE.md |
0 commit comments