### Install Laravel Settings Package Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Installs the Laravel Settings package using Composer and publishes necessary configuration and migration files. ```bash composer require ruangdeveloper/laravel-settings php artisan vendor:publish --provider="RuangDeveloper\LaravelSettings\LaravelSettingsServiceProvider" --tag="config" php artisan vendor:publish --provider="RuangDeveloper\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" ``` -------------------------------- ### Get a Global Setting Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Illustrates retrieving a global setting value with `Settings::get()`, including how to provide a default fallback value. ```php use RuangDeveloper\LaravelSettings\Facades\Settings; // retrieve the global site title $siteTItle = Settings::get('site_title'); // you may want to add a default fallback value if the setting // with provided key doesn't exists in the database $siteTitle = Settings::get('site_title', 'Your Default Awesome Website'); ``` -------------------------------- ### Get Setting from Model Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Retrieves a setting value associated with a model instance using its key. If the setting does not exist, it will return null. ```php $user = App\Models\User::find(1); $isSubscribed = $user->getSetting('subscribe_newsletter'); ``` -------------------------------- ### Get Setting with Type Casting Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Shows how to retrieve a setting and cast it to a specific data type using `Settings::getAs()` or specific type methods like `getString()`, `getInteger()`, etc. ```php use RuangDeveloper\LaravelSettings\Enums\Type; use RuangDeveloper\LaravelSettings\Facades\Settings; // retrieve the global site title and cast to String $siteTitle = Settings::getAs('site_title', Type::String); // retrieve with default and cast to String $siteTitle = Settings::getAs('site_title', Type::String, 'Your Default Awesome Website'); // Example using specific type getter $siteTitle = Settings::getString('site_title', 'Default Title'); ``` -------------------------------- ### Get Setting as Specific Type (Direct Methods) Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Provides direct helper methods to retrieve settings and cast them to common types like String, Integer, Float, Boolean, Array, or Object. ```php $yourModel->getSettingString('key', $default); $yourModel->getSettingInteger('key', $default); $yourModel->getSettingFloat('key', $default); $yourModel->getSettingBoolean('key', $default); $yourModel->getSettingArray('key', $default); $yourModel->getSettingObject('key', $default); ``` -------------------------------- ### Get Setting with Default Value Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Retrieves a setting value from a model instance, providing a fallback default value if the setting key is not found in the database. ```php $user = App\Models\User::find(1); $isSubscribed = $user->getSetting('subscribe_newsletter', false); ``` -------------------------------- ### Get Setting Cast with Default Value Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Retrieves a setting value, casts it to a specified type, and provides a default value if the setting is not found. ```php use RuangDeveloper\LaravelSettings\Enums\Type; $user = App\Models\User::find(1); $isSubscribed = $user->getSettingAs('subscribe_newsletter', Type::Boolean, false); ``` -------------------------------- ### Get Setting Cast to Specific Type Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Retrieves a setting value and casts it to a specified data type using the `getSettingAs` method. This is useful for ensuring data integrity. ```php use RuangDeveloper\LaravelSettings\Enums\Type; $user = App\Models\User::find(1); $isSubscribed = $user->getSettingAs('subscribe_newsletter', Type::Boolean); ``` -------------------------------- ### Set a Global Setting Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Demonstrates how to set a global application setting using the `Settings::set()` method. ```php use RuangDeveloper\LaravelSettings\Facades\Settings; // setting the global site title Settings::set('site_title', 'Your Awesome Website'); ``` -------------------------------- ### Laravel Settings API Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Provides methods for managing application settings using the Settings facade. Supports setting, retrieving, and deleting global settings with optional type casting and default values. ```APIDOC Settings Facade Methods: __construct() Initializes the settings manager. set(string $key, mixed $value): void Sets a global setting value. - Parameters: - key: The unique identifier for the setting. - value: The value to store for the setting. get(string $key, mixed $default = null): mixed Retrieves a global setting value. - Parameters: - key: The unique identifier for the setting. - default: The value to return if the setting is not found. - Returns: The setting value or the default value. getAs(string $key, Type $type, mixed $default = null): mixed Retrieves a global setting value and casts it to a specified type. - Parameters: - key: The unique identifier for the setting. - type: The desired data type (e.g., Type::String, Type::Integer). - default: The value to return if the setting is not found. - Returns: The setting value cast to the specified type or the default value. getString(string $key, mixed $default = null): string Retrieves a global setting value and casts it to a string. getInteger(string $key, mixed $default = null): int Retrieves a global setting value and casts it to an integer. getFloat(string $key, mixed $default = null): float Retrieves a global setting value and casts it to a float. getBoolean(string $key, mixed $default = null): bool Retrieves a global setting value and casts it to a boolean. getArray(string $key, mixed $default = null): array Retrieves a global setting value and casts it to an array. getObject(string $key, mixed $default = null): object Retrieves a global setting value and casts it to an object. delete(string $key): void Deletes a global setting. - Parameters: - key: The unique identifier for the setting to delete. forget(string $key): void Deprecated alias for delete(). - Parameters: - key: The unique identifier for the setting to delete. ``` -------------------------------- ### Retrieve Default Global Setting Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Demonstrates how to retrieve a global setting using the `Settings` facade. If the setting is not found in the database, the configured default value is returned. ```php use RuangDeveloper\LaravelSettings\Facades\Settings; $siteTItle = Settings::get('site_title'); echo $siteTitle; // Outputs 'Your Awesome Site' if not set in DB ``` -------------------------------- ### Configure Cache Settings Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Enables and configures caching for settings to reduce database queries. Settings can be stored in the cache with a specified prefix and lifetime. ```php true, // The cache key prefix that will be used to store the settings in the cache. 'cache_prefix' => 'laravel-settings', // Cache lifetime in seconds. 'cache_lifetime' => 60 * 60 * 24 * 7, // 7 days // other config ]; ``` -------------------------------- ### Set Setting on Model Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md This snippet demonstrates how to set a specific setting for a model instance. The setting is stored with a key-value pair. ```php $user = App\Models\User::find(1); $user->setSetting('subscribe_newsletter', true); ``` -------------------------------- ### Configure Custom Setting Model Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Instructs the package to use your custom `Setting` model by specifying its class name in the `config/laravel-settings.php` file. ```php return [ // others config 'model' => App\Models\CustomSetting::class, ]; ``` -------------------------------- ### Extend Setting Model Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Allows customization by extending the package's default `Setting` model. You can create your own model to add custom logic or fields. ```php use RuangDeveloper\LaravelSettings\Models\Setting; class CustomSetting extends Setting { // } ``` -------------------------------- ### Configure Default Global Settings Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Allows defining default values for global settings within the `config/laravel-settings.php` file. These defaults are used when a setting is not found in the database. ```php return [ // others config 'defaults' => [ 'site_title' => 'Your Awesome Site', ], ]; ``` -------------------------------- ### Retrieve Default Model Specific Setting Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Shows how to retrieve a setting for a model instance. If the setting is not found for that specific instance, the default value defined in `model_defaults` is returned. ```php $user = App\Models\User::find(1); $isSubscribed = $user->getSetting('subscribe_newsletter'); echo $isSubscribed // Outputs false if not set for the user ``` -------------------------------- ### Delete a Global Setting Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Demonstrates how to remove a global setting from the database using the `Settings::delete()` method. ```php use RuangDeveloper\LaravelSettings\Facades\Settings; Settings::delete('site_title'); ``` -------------------------------- ### Forget a Global Setting (Deprecated) Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Shows the deprecated `Settings::forget()` method, which is functionally equivalent to `Settings::delete()`. ```php use RuangDeveloper\LaravelSettings\Facades\Settings; Settings::forget('site_title'); ``` -------------------------------- ### Configure Model with HasSettings Trait Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md To link settings to a specific model, you must use the `HasSettings` trait within your Eloquent model. This enables the model to manage its own settings. ```php use Illuminate\Database\Eloquent\Model; use RuangDeveloper\LaravelSettings\Traits\HasSettings; class User extends Model { use HasSettings; // use the HasSettings trait protected $guarded = []; } ``` -------------------------------- ### Configure Default Model Specific Settings Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Enables setting default values for specific models. These defaults are applied when a model instance attempts to retrieve a setting that hasn't been explicitly set for it. ```php return [ // others config 'model_defaults' => [ 'App\Models\User' => [ 'subscribe_newsletter' => false ], ], ]; ``` -------------------------------- ### Forget Setting from Model (Deprecated) Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md This method is deprecated and serves the same purpose as `deleteSetting`. It is recommended to use `deleteSetting` for clarity and future compatibility. ```php $user = App\Models\User::find(1); $user->forgetSetting('subscribe_newsletter'); ``` -------------------------------- ### Delete Setting from Model Source: https://github.com/ruangdeveloper/laravel-settings/blob/main/README.md Removes a specific setting from a model instance by its key. This permanently deletes the setting from the database. ```php $user = App\Models\User::find(1); $user->deleteSetting('subscribe_newsletter'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.