### Install Laravel Setting Pro and Run Migrations Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md This Artisan command completes the installation of the Laravel Setting Pro package by running necessary database migrations and any other setup operations. It ensures the settings table is created and ready for use. ```bash php artisan setting:install ``` -------------------------------- ### Complete Usage Example: Settings Controller Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt This PHP code demonstrates a comprehensive example of settings management within a Laravel application using the `SajadsdiLaravelSettingProSupportSetting` class. It includes methods for displaying settings, updating multiple settings at once, and resetting settings to their default values. Input validation is performed before updating settings. ```php get(); return view('settings.index', [ 'theme' => $settings['theme'] ?? 'light', 'language' => $settings['locale'] ?? 'en', 'notifications' => $settings['notifications'] ?? [], ]); } public function update(Request $request) { $validated = $request->validate([ 'theme' => 'required|in:light,dark,auto', 'language' => 'required|string|size:2', 'notifications.email' => 'boolean', 'notifications.push' => 'boolean', ]); // Update multiple settings at once setting('app_settings')->set([ 'theme' => $validated['theme'], 'locale' => $validated['language'], 'notifications.email' => $validated['notifications']['email'] ?? false, 'notifications.push' => $validated['notifications']['push'] ?? false, ]); return redirect()->back()->with('success', 'Settings updated successfully'); } public function resetToDefaults() { // Delete custom settings and restore defaults setting('app_settings')->delete(['theme', 'locale', 'notifications']); // Set default values setting('app_settings')->set([ 'theme' => 'light', 'locale' => 'en', 'notifications.email' => true, 'notifications.push' => true, ]); return redirect()->back()->with('success', 'Settings reset to defaults'); } } ``` -------------------------------- ### Install Laravel Setting Pro via Composer Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md This command installs the Laravel Setting Pro package using Composer. Ensure you have Composer installed and configured for your Laravel project. ```bash composer require sajadsdi/laravel-setting-pro ``` -------------------------------- ### Publish Configuration and Migrations Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md This Artisan command publishes the package's configuration file (`_setting.php`) and migration file to your Laravel project. This is a necessary step before running the installation command. ```bash php artisan setting:publish ``` -------------------------------- ### Advanced Get, Set, Delete, and Has Operations with Dot Notation (PHP) Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md Demonstrates advanced usage of the `Setting` facade and `setting()` helper for retrieving, setting, deleting, and checking the existence of multiple configuration keys using dot notation. This functionality relies on the 'Dot Notation Array' package. ```php //get operation $value = Setting::my_setting(['users.3.profile.pic','users.3.profile.name'], ["default.png","No name"]); //or multi keys and single defaults $value = setting('my_setting')->get(['users.3.profile.pic','users.3.profile.name'], ["no data"]); //set operation setting::select('my_setting')->set(['users.3.profile.pic' => "profile.png",'users.3.profile.name' => "john"]) //delete multiple keys setting::select('my_setting')->delete(['users.3.profile.pic','users.3.profile.name']); //multiple keys checking exists by has method if(Setting::select('my_setting')->has(['users.3.profile.pic','users.3.profile.name'])){ echo "The keys are exists!"; }else{ echo "The keys do not exist!"; } // it's very Easy ``` -------------------------------- ### Get Setting Value using `Setting` Facade Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md Demonstrates retrieving setting values using the `Setting` facade. This method provides an alternative to the global helper function and offers similar flexibility in accessing settings. ```php get('key', 'default value'); //or $value = Setting::my_setting()->get('key', 'default value'); //or $value = Setting::my_setting('key', 'default value'); ``` -------------------------------- ### Use Settings in Blade Templates Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt This example illustrates how to access and display settings directly within your Blade templates. The `setting()` helper function can be used to retrieve individual setting values, with default fallbacks. It also demonstrates how to check for the existence of a setting using `setting()->has()` before attempting to display its content. ```php // resources/views/layouts/app.blade.php
@if(setting('app_settings')->has('custom_css')) @endif ``` -------------------------------- ### Get Setting Value with Facade (PHP) Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt Retrieves setting values using the `Setting` facade. Offers various syntaxes including `select()`, dynamic method calls, and shorthand argument passing. ```php use Sajadsdi\LaravelSettingPro\Support\Setting; // Using select() method $currency = Setting::select('store_config')->get('currency', 'USD'); // Returns: 'EUR' // Dynamic method syntax - setting name as method $language = Setting::app_settings()->get('locale', 'en'); // Returns: 'fr' // Shortest syntax - setting name and key as method arguments $maxUpload = Setting::app_settings('uploads.max_size', 5242880); // Returns: 10485760 // Get entire setting as array (no key specified) $allSettings = Setting::select('app_settings')->get(); // Returns: ['theme' => 'dark', 'locale' => 'fr', 'uploads' => [...]] ``` -------------------------------- ### Get Setting Value using `setting` function Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md Demonstrates how to retrieve a setting value using the global `setting` helper function. It shows multiple ways to access settings, including specifying a default value and selecting a specific driver. ```php // Get a setting value $value = setting('my_setting')->get('key', 'default value'); //or $value = setting('my_setting','key', 'default value'); //or $value = setting()->select('my_setting')->get('key', 'default value'); ``` -------------------------------- ### Use Settings in Laravel Config Files Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt This example shows how to dynamically use settings managed by Laravel Setting Pro within your application's configuration files, such as `config/mail.php`. The `setting()` helper function allows you to retrieve a setting value, with an optional default value if the setting does not exist. ```php // config/mail.php return [ 'default' => setting('mail_config', 'driver', 'smtp'), 'mailers' => [ 'smtp' => [ 'host' => setting('mail_config', 'smtp.host', 'smtp.mailgun.org'), 'port' => setting('mail_config', 'smtp.port', 587), 'username' => setting('mail_config', 'smtp.username', ''), 'password' => setting('mail_config', 'smtp.password', ''), ], ], ]; ``` -------------------------------- ### Set Setting Value using `Setting` Facade Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md Illustrates setting or updating configuration values through the `Setting` facade. This approach allows for programmatic modification of application settings. ```php // Set a setting value Setting::select('my_setting')->set('key', 'value'); //or Setting::my_setting()->set('key', 'value'); ``` -------------------------------- ### Check if Setting Key Exists using `Setting` Facade Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md Demonstrates how to check for the existence of a setting key using the `has` method of the `Setting` facade. This is useful for conditional logic based on whether a setting is defined. ```php //checking exists by has method if(Setting::select('my_setting')->has('key')){ echo "key exists!"; }else{ echo "key not exists!"; } ``` -------------------------------- ### Set Setting Value using `setting` function Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md Illustrates how to set or update setting values using the global `setting` helper function. You can pass an array of key-value pairs or set individual keys. ```php // Set a setting value setting('my_setting')->set(['key' => 'value']); //or setting()->select('my_setting')->set(['key' => 'value']); ``` -------------------------------- ### Publish MongoDB Migration (Optional) Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt Publishes the migration file specifically for MongoDB storage. This is only required if you intend to use MongoDB as your database driver for settings. ```bash php artisan setting:publish-mongodb php artisan migrate ``` -------------------------------- ### Configure Queue and Event Handling for Settings Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt This configuration file enables background processing for setting changes by utilizing queues and triggers events on setting modifications. It specifies the queue name for background processing and a boolean flag to enable event triggering. ```php // config/_setting.php return [ // Process updates/deletes in background queue 'process_to_queue' => true, 'queue' => 'settings', // Trigger events on setting changes 'trigger_events' => true, ]; ``` -------------------------------- ### Set Setting Value with Helper Function and Facade (PHP) Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt Sets one or more application settings using either the `setting()` helper function or the `Setting` facade. Supports single values, multiple values, and nested keys via dot notation. ```php // Set single value with helper function setting('app_settings')->set(['theme' => 'dark']); // Set with key-value syntax setting('app_settings')->set('api.timeout', 120); // Set multiple values at once setting('user_preferences')->set([ 'notifications.email' => true, 'notifications.push' => false, 'theme' => 'dark', 'language' => 'en' ]); // Set nested values using dot notation setting('users')->set([ 'users.42.profile.name' => 'John Doe', 'users.42.profile.email' => 'john@example.com', 'users.42.profile.avatar' => 'uploads/avatar-42.jpg' ]); // Using Setting facade use Sajadsdi\LaravelSettingPro\Support\Setting; Setting::select('store_config')->set('currency', 'EUR'); Setting::store_config()->set(['tax_rate' => 0.21, 'shipping.default' => 5.99]); ``` -------------------------------- ### Configure Storage Settings in PHP Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt Defines the default storage driver ('file' or 'database') and the fallback driver for importing missing settings within the `config/_setting.php` file. It specifies paths for file storage and connection details for database drivers, including support for MySQL and MongoDB. ```php // config/_setting.php return [ 'store' => [ // Default storage driver: 'file' or 'database' 'default' => 'file', // Fallback driver for importing missing settings 'import_from' => 'database', 'drivers' => [ 'file' => [ 'path' => base_path('setting'), 'class' => \Sajadsdi\LaravelSettingPro\Drivers\File::class, ], 'database' => [ 'connection' => 'mysql', // or 'mongodb' 'class' => \Sajadsdi\LaravelSettingPro\Drivers\Database::class, 'connections' => [ 'mysql' => [ 'class' => \Sajadsdi\LaravelSettingPro\Drivers\Database\Mysql::class, 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'prefix' => '', ], 'mongodb' => [ 'class' => \Sajadsdi\LaravelSettingPro\Drivers\Database\MongoDB::class, 'host' => env('MONGO_DB_HOST', '127.0.0.1'), 'port' => env('MONGO_DB_PORT', 27017), 'database' => env('MONGO_DB_DATABASE', 'forge'), 'username' => env('MONGO_DB_USERNAME', ''), 'password' => env('MONGO_DB_PASSWORD', ''), 'options' => [], ], ], ], ], ], ]; ``` -------------------------------- ### Configure Laravel Setting Pro Storage and Cache Drivers Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/CONFIGURATION.md This PHP configuration file defines how the Laravel Setting Pro package stores and caches settings. It allows for different storage drivers like 'file' and 'database', and cache drivers like 'file' and 'redis'. The 'store' section manages default storage and import options, while 'cache' enables or disables caching and specifies cache drivers. Background processing and event triggering are also configurable. ```php return [ 'store' => [ //Default setting store driver 'default' => 'file', //If any requested setting is not found in the default driver, //this driver is used and if available, it is imported into //the default driver. you can set empty to disable this feature. 'import_from' => '', //Include drivers and configures for setting store 'drivers' => [ 'file' => [ 'path' => base_path('setting'), 'class' => \Sajadsdi\LaravelSettingPro\Drivers\File::class, ], 'database' => [ //Default database connection if default store is 'database' 'connection' => 'mysql', 'class' => \Sajadsdi\LaravelSettingPro\Drivers\Database::class, 'connections' => [ 'mysql' => [ 'class' => \Sajadsdi\LaravelSettingPro\Drivers\Database\Mysql::class, 'driver' => "mysql", 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'prefix' => '', ], 'mongodb' => [ 'class' => \Sajadsdi\LaravelSettingPro\Drivers\Database\MongoDB::class, 'host' => env('MONGO_DB_HOST', '127.0.0.1'), 'port' => env('MONGO_DB_PORT', 27017), 'database' => env('MONGO_DB_DATABASE', 'forge'), 'username' => env('MONGO_DB_USERNAME', ''), 'password' => env('MONGO_DB_PASSWORD', ''), 'options' => [ ], ] ] ], ], ], 'cache' => [ //flag for enable or disable cache. 'enabled' => false, //Default setting cache driver 'default' => 'file', 'class' => \Sajadsdi\LaravelSettingPro\Cache\Cache::class, //Include drivers and configures for setting cache 'drivers' => [ 'file' => [ 'class' => \Sajadsdi\LaravelSettingPro\Cache\Drivers\File::class, 'path' => storage_path('framework/cache/settings'), ], 'redis' => [ 'class' => \Sajadsdi\LaravelSettingPro\Cache\Drivers\Redis::class, 'connection' => [ 'scheme' => 'tcp', 'host' => env('REDIS_HOST', '127.0.0.1'), 'username' => env('REDIS_USERNAME'), 'password' => env('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_CACHE_DB', '1'), ], 'options' => [ 'cluster' => env('REDIS_CLUSTER', 'redis'), 'prefix' => 'setting_caches_', ], ] ] ], //If true ,update and delete Settings handled on background. 'process_to_queue' => false, 'queue' => 'settings', //If true ,trigger events after update and delete settings. you can create listener for: //Sajadsdi\LaravelSettingPro\Events\UpdateSettingEvent //Sajadsdi\LaravelSettingPro\Events\DeleteSettingEvent 'trigger_events' => false ]; ``` -------------------------------- ### Clearing Settings Cache (Bash) Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md Command to clear the settings cache for the Laravel Setting Pro package. This is useful after making changes to configuration or settings that are cached for performance. ```bash php artisan setting:clear-cache ``` -------------------------------- ### Create Event Listener for Setting Deletions Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt This PHP class acts as an event listener for the `DeleteSettingEvent` from the Laravel Setting Pro package. It logs a warning message when setting keys are deleted, including the setting name and the specific keys that were removed. It also logs the data that existed before the deletion occurred. ```php // app/Listeners/SettingDeletedListener.php namespace App\Listeners; use Sajadsdi\LaravelSettingPro\Events\DeleteSettingEvent; class SettingDeletedListener { public function handle(DeleteSettingEvent $event): void { // $event->settingName - Name of the setting // $event->deletedKeys - Array of keys that were deleted // $event->oldData - Data before deletion logger()->warning('Setting keys deleted', [ 'setting' => $event->settingName, 'deleted_keys' => $event->deletedKeys, ]); } } ``` -------------------------------- ### Create Event Listener for Setting Updates Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt This PHP class serves as an event listener for the `UpdateSettingEvent` provided by the Laravel Setting Pro package. It logs information about the setting update, including the setting name, changed keys and values, and previous data. It also includes logic to clear a specific cache entry when the 'app_settings' theme is updated. ```php // app/Listeners/SettingUpdatedListener.php namespace App\Listeners; use Sajadsdi\LaravelSettingPro\Events\UpdateSettingEvent; class SettingUpdatedListener { public function handle(UpdateSettingEvent $event): void { // $event->settingName - Name of the setting that was updated // $event->seatedKeyValues - Array of keys and values that were set // $event->oldData - Previous data before the update logger()->info('Setting updated', [ 'setting' => $event->settingName, 'changes' => $event->seatedKeyValues, 'previous' => $event->oldData, ]); if ($event->settingName === 'app_settings' && isset($event->seatedKeyValues['theme'])) { cache()->forget('compiled_theme_css'); } } } ``` -------------------------------- ### Check Setting Key Existence in PHP Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt Verifies if one or more keys exist within a setting group. Supports single keys, nested keys, and multiple keys, returning true only if all specified keys are present. It can be used with the global `setting()` helper or the `Setting` facade. ```php // Check single key if (setting('app_settings')->has('api_key')) { $apiKey = setting('app_settings')->get('api_key'); // Use the API key } // Check nested key $hasProfile = setting('users')->has('users.42.profile'); // Returns: true or false // Check multiple keys (returns true only if ALL keys exist) if (setting('payment_config')->has(['stripe.key', 'stripe.secret', 'stripe.webhook'])) { // All Stripe credentials are configured initializeStripePayments(); } // Using Setting facade use Sajadsdi\LaravelSettingPro\Support\Setting; if (Setting::select('features')->has('dark_mode')) { enableDarkModeToggle(); } if (Setting::app_settings()->has(['smtp.host', 'smtp.port', 'smtp.username'])) { configureMailer(); } ``` -------------------------------- ### Delete Setting Key using `Setting` Facade Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md Shows how to remove a setting key using the `Setting` facade. This operation is useful for cleaning up or removing specific configuration parameters. ```php //delete key from setting Setting::select('my_setting')->delete('key'); //or Setting::my_setting()->delete('key'); ``` -------------------------------- ### Configure Cache Settings in PHP Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt Enables and configures caching for improved performance within the `config/_setting.php` file. It allows selection of cache drivers like 'file' or 'redis' and specifies connection details and options for each driver. ```php // config/_setting.php return [ 'cache' => [ // Enable/disable caching 'enabled' => true, // Cache driver: 'file' or 'redis' 'default' => 'redis', 'class' => \Sajadsdi\LaravelSettingPro\Cache\Cache::class, 'drivers' => [ 'file' => [ 'class' => \Sajadsdi\LaravelSettingPro\Cache\Drivers\File::class, 'path' => storage_path('framework/cache/settings'), ], 'redis' => [ 'class' => \Sajadsdi\LaravelSettingPro\Cache\Drivers\Redis::class, 'connection' => [ 'scheme' => 'tcp', 'host' => env('REDIS_HOST', '127.0.0.1'), 'username' => env('REDIS_USERNAME'), 'password' => env('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', '6379'), 'database' => env('REDIS_CACHE_DB', '1'), ], 'options' => [ 'cluster' => env('REDIS_CLUSTER', 'redis'), 'prefix' => 'setting_caches_', ], ], ], ], ]; ``` -------------------------------- ### Delete Setting Key using `setting` function Source: https://github.com/sajadsdi/laravel-setting-pro/blob/main/README.md Shows how to remove a specific setting key using the global `setting` helper function. This operation deletes the key-value pair from the specified setting driver. ```php //delete a key from setting setting('my_setting')->delete('key'); //or setting()->select('my_setting')->delete('key'); ``` -------------------------------- ### Delete Setting Keys with Helper Function and Facade (PHP) Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt Removes specific setting keys, nested keys, multiple keys, or entire settings using dot notation. This can be done via the `setting()` helper function or the `Setting` facade. ```php // Delete single key setting('app_settings')->delete('deprecated_feature'); // Delete nested key setting('users')->delete('users.42.profile.temp_data'); // Delete multiple keys at once setting('app_settings')->delete([ 'old_api_key', 'legacy.feature_flag', 'cache.old_prefix' ]); // Delete entire setting (all keys) setting('temp_settings')->delete(); // Using Setting facade use Sajadsdi\LaravelSettingPro\Support\Setting; Setting::select('user_preferences')->delete('notifications.sms'); Setting::user_preferences()->delete(['theme', 'custom_css']); ``` -------------------------------- ### Register Setting Event Listeners in EventServiceProvider Source: https://context7.com/sajadsdi/laravel-setting-pro/llms.txt This code snippet demonstrates how to register the custom event listeners for setting updates and deletions within Laravel's `EventServiceProvider`. It maps the `UpdateSettingEvent` and `DeleteSettingEvent` classes to their respective listener classes (`SettingUpdatedListener` and `SettingDeletedListener`). ```php // app/Providers/EventServiceProvider.php protected $listen = [ \Sajadsdi\LaravelSettingPro\Events\UpdateSettingEvent::class => [ \App\Listeners\SettingUpdatedListener::class, ], \Sajadsdi\LaravelSettingPro\Events\DeleteSettingEvent::class => [ \App\Listeners\SettingDeletedListener::class, ], ]; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.