PHP code example of bleuren / laravel-settings

1. Go to this page and download the library: Download bleuren/laravel-settings library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bleuren / laravel-settings example snippets


use Bleuren\LaravelSetting\Facades\Setting;

// 獲取設定值
$appName = Setting::get('app.name', 'Default App');

// 設置設定值
Setting::set('app.name', 'My Application', '應用程式名稱');

// 批量設置
Setting::setMany([
    'app.name' => 'My App',
    'app.theme' => 'dark',
    'app.timezone' => 'Asia/Taipei',
    'maintenance.mode' => false
], '應用程式基本設定');

// 檢查設定是否存在
if (Setting::has('app.name')) {
    // 執行相關邏輯
}

// 刪除設定
Setting::remove('old.setting');

// 搜索設定
$appSettings = Setting::search('app.%');

// 獲取所有設定
$allSettings = Setting::all();

// 簡潔的獲取方式
$appName = setting('app.name', 'Default App');
$theme = setting('app.theme');

use Bleuren\LaravelSetting\Contracts\SettingRepository;

class UserController extends Controller
{
    public function __construct(
        private SettingRepository $settings
    ) {}

    public function updateProfile(Request $request)
    {
        // 使用注入的設定服務
        $defaultTheme = $this->settings->get('user.default_theme', 'light');
        
        // 更新使用者偏好設定
        $this->settings->setMany([
            'user.theme' => $request->theme,
            'user.language' => $request->language,
        ], '使用者偏好設定');
    }
}



namespace App\Models;

use Bleuren\LaravelSetting\Traits\HasSettings;
use Illuminate\Database\Eloquent\Model;

class UserSetting extends Model
{
    use HasSettings;

    protected $table = 'user_settings';
    
    protected $fillable = [
        'key', 'value', 'description',
        'user_id', 'category', 'is_public'
    ];

    protected $casts = [
        'is_public' => 'boolean',
    ];

    // 自定義關聯
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    // 自定義查詢方法
    public function getSettingsByCategory(string $category)
    {
        return $this->where('category', $category)->get();
    }

    public function getPublicSettings()
    {
        return $this->where('is_public', true)->get();
    }
}

// config/settings.php
return [
    'model' => App\Models\UserSetting::class,
    'table' => 'user_settings',
    'database_connection' => 'mysql',
    'cache_prefix' => 'user_settings.',
    
    // 其他配置...
];

// 透過 Facade(會使用配置的模型)
Setting::set('notification.email', true);

// 直接使用模型靜態方法
UserSetting::set('theme.color', 'blue');
$hasNotification = UserSetting::has('notification.email');

// 使用自定義方法
$publicSettings = UserSetting::getPublicSettings();
$categorySettings = UserSetting::getSettingsByCategory('appearance');

// config/settings.php
return [
    // 設定模型類別
    'model' => env('SETTINGS_MODEL', \Bleuren\LaravelSetting\Setting::class),

    // 資料庫配置
    'database_connection' => env('SETTINGS_DB_CONNECTION', null),
    'table' => env('SETTINGS_TABLE', 'settings'),

    // 緩存配置
    'cache_prefix' => env('SETTINGS_CACHE_PREFIX', 'settings.'),

    // 預載入配置
    'eager_load' => env('SETTINGS_EAGER_LOAD', false),
    'eager_load_keys' => [
        'app.name',
        'app.theme',
        'app.timezone',
        // 添加常用的設定鍵...
    ],

    // 效能配置
    'batch_size' => env('SETTINGS_BATCH_SIZE', 100),
    'enable_query_log' => env('SETTINGS_ENABLE_QUERY_LOG', false),
];

// config/settings.php
'eager_load' => true,
'eager_load_keys' => [
    'app.name',
    'app.logo',
    'app.theme',
    'mail.from_address',
    'social.facebook_url',
],

// 高效的批量操作
$settings = [
    'mail.driver' => 'smtp',
    'mail.host' => 'smtp.gmail.com',
    'mail.port' => 587,
    'mail.encryption' => 'tls',
];

// 使用事務確保一致性
Setting::setMany($settings, '郵件服務設定');

// 搜索所有應用相關設定
$appSettings = Setting::search('app.%');

// 搜索所有郵件設定
$mailSettings = Setting::search('mail.%');

// 使用自定義模型的進階搜索
$publicSettings = UserSetting::getPublicSettings();
$categorySettings = UserSetting::getSettingsByCategory('appearance');

// 在服務提供者中註冊
$this->app->bind(SettingRepository::class, SettingManager::class);

// 在控制器中使用
public function __construct(SettingRepository $settings) {
    $this->settings = $settings;
}

// 舊的設定套件
Settings::set('key', 'value');
$value = Settings::get('key');

// Laravel Settings(相容的 API)
Setting::set('key', 'value');
$value = Setting::get('key');
bash
php artisan vendor:publish --tag=laravel-settings-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=laravel-settings-config