├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── composer.lock ├── config └── config.php ├── database └── migrations │ ├── 2022_12_11_111047_create_settings_groups_table.php │ ├── 2022_12_11_121047_create_settings_table.php │ ├── 2022_12_27_234647_change_value_to_text_in_settings.php │ └── 2023_02_15_234647_make_value_nullable_in_settings.php ├── lang ├── en │ └── ui.php ├── ro │ └── ui.php └── ru │ └── ui.php └── src ├── Events ├── LSSSettingChangedEvent.php └── LSSSettingGroupChangedEvent.php ├── Facades └── LaravelSiteSettings.php ├── Filament └── Resources │ ├── SettingGroupResource.php │ ├── SettingGroupResource │ └── Pages │ │ ├── CreateSettingGroup.php │ │ ├── EditSettingGroup.php │ │ └── ListSettingGroups.php │ ├── SettingResource.php │ └── SettingResource │ └── Pages │ ├── CreateSetting.php │ ├── EditSetting.php │ └── ListSettings.php ├── FilamentLaravelSiteSettingsProvider.php ├── LaravelSiteSettings.php ├── LaravelSiteSettingsProvider.php ├── Models ├── Setting.php └── SettingGroup.php ├── MoonShine └── Resources │ ├── SettingGroupResource.php │ └── SettingResource.php ├── Observers └── LSSObserver.php └── helpers.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | /vendor/ 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Visual Ideas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Easy laravel cached settings 2 | 3 | Easy laravel cached settings (stored in MYSQL) package with MoonShine/Filament Laravel Admin GUI 4 | 5 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/visual-ideas/laravel-site-settings.svg?style=flat-square)](https://packagist.org/packages/visual-ideas/laravel-site-settings) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/visual-ideas/laravel-site-settings.svg?style=flat-square)](https://packagist.org/packages/visual-ideas/laravel-site-settings) 7 | 8 | ## Installation 9 | 10 | You can install the package via composer: 11 | 12 | ```bash 13 | composer require visual-ideas/laravel-site-settings 14 | ``` 15 | 16 | You must run the migrations with: 17 | 18 | ```bash 19 | php artisan migrate 20 | ``` 21 | 22 | You can publish the config file with: 23 | 24 | ```bash 25 | php artisan vendor:publish --provider="VI\LaravelSiteSettings\LaravelSiteSettingsProvider" --tag="config" 26 | ``` 27 | 28 | This is the contents of the published config file: 29 | 30 | ```php 31 | return [ 32 | 'cache_key' => env('LSS_CACHE_KEY','laravel_site_settings_data'), 33 | 34 | // Set to true if you're using Filament (https://filamentphp.com/) 35 | 'filament' => false, 36 | ]; 37 | ``` 38 | 39 | ## Usage 40 | 41 | You can use this package as default laravel config() function! 42 | 43 | ```php 44 | function settings($key = null, $default = null) 45 | { 46 | if (is_null($key)) { 47 | return app('Settings')->all(); 48 | } 49 | 50 | if (is_array($key)) { 51 | return app('Settings')->set($key); 52 | } 53 | 54 | return app('Settings')->get($key, $default); 55 | } 56 | ``` 57 | 58 | or Blade directive @settings 59 | 60 | ```php 61 | @settings('group.setting') 62 | ``` 63 | For PHPStorm you can set this blade directive with [This instruction](https://www.jetbrains.com/help/phpstorm/blade-page.html) 64 | 65 | 66 | or as part of native Laravel config() 67 | 68 | ```php 69 | @config('settings.group.setting') 70 | ``` 71 | 72 | Not working in console! 73 | 74 | ## Update settings 75 | 76 | You can use models VI\LaravelSiteSettings\Models\SettingGroup and VI\LaravelSiteSettings\Models\Setting 77 | 78 | or set settings values with the settings() function: 79 | 80 | ```php 81 | settings(['group.setting' => 'Value']); 82 | settings(['setting' => 'Value']); 83 | ``` 84 | 85 | ## Usage with MoonShine Laravel Admin panel 86 | 87 | Please see [MoonShine](https://moonshine.cutcode.ru/) 88 | 89 | You can use settings in your MoonShine admin panel, like this: 90 | 91 | ```php 92 | MenuGroup::make('Settings', [ 93 | MenuItem::make( 94 | 'Setting groups', 95 | new \VI\LaravelSiteSettings\MoonShine\Resources\SettingGroupResource(), 96 | 'heroicons.outline.wrench-screwdriver' 97 | ), 98 | MenuItem::make( 99 | 'Settings', 100 | new \VI\LaravelSiteSettings\MoonShine\Resources\SettingResource(), 101 | 'heroicons.outline.wrench' 102 | ), 103 | ], 'heroicons.outline.cog-8-tooth'), 104 | ``` 105 | 106 | ## Usage with Filament Laravel Admin panel 107 | 108 | Please see [Filament](https://filamentphp.com) 109 | 110 | You can use settings in your Filament admin panel! 111 | 112 | Just change config file: 113 | 114 | ```php 115 | //... 116 | // Set to true if you're using Filament (https://filamentphp.com/) 117 | 'filament' => true, 118 | //... 119 | ``` 120 | 121 | ## Seeding settings 122 | 123 | I recommend saving the settings in the seeders using the [orangehill/iseed](https://github.com/orangehill/iseed) package: 124 | 125 | ```bash 126 | php artisan iseed setting_groups,settings 127 | ``` 128 | 129 | But you can use seeder or migration to set your settings 130 | 131 | ```php 132 | settings([ 133 | ['group.setting1' => 'Value1'], 134 | ['group.setting2' => 'Value2'], 135 | ['group.setting3' => 'Value3'], 136 | ['setting1' => 'Value4'], 137 | ['setting2' => 'Value5'], 138 | ['setting3' => 'Value6'], 139 | ['setting4' => 'Value7'], 140 | ['setting5' => 'Value8'] 141 | ]); 142 | ``` 143 | 144 | ## Credits 145 | 146 | - [Alex](https://github.com/alexvenga) 147 | 148 | ## License 149 | 150 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 151 | 152 | 153 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "visual-ideas/laravel-site-settings", 3 | "description": "Easy Laravel nested settings (stored in MYSQL) package with MoonShine Laravel Admin GUI", 4 | "keywords": [ 5 | "Settings", 6 | "Laravel", 7 | "Package", 8 | "PHP", 9 | "MySQL", 10 | "MoonShine", 11 | "Filament", 12 | "VisualIdeas" 13 | ], 14 | "homepage": "https://github.com/visual-ideas/laravel-site-settings", 15 | "license": "MIT", 16 | "authors": [ 17 | { 18 | "name": "Alex", 19 | "email": "alex.visualideas@gmail.com", 20 | "role": "Developer" 21 | } 22 | ], 23 | 24 | "require": { 25 | "php": "^8.1", 26 | "laravel/framework": "^9.21|^10.0", 27 | "doctrine/dbal": "^3.5" 28 | }, 29 | 30 | "autoload": { 31 | "psr-4": { 32 | "VI\\LaravelSiteSettings\\": "src" 33 | }, 34 | "files": [ 35 | "src/helpers.php" 36 | ] 37 | }, 38 | "extra": { 39 | "laravel": { 40 | "providers": [ 41 | "VI\\LaravelSiteSettings\\LaravelSiteSettingsProvider" 42 | ], 43 | "aliases": { 44 | "LaravelSiteSettings": "VI\\LaravelSiteSettings\\Facades\\LaravelSiteSettings" 45 | } 46 | } 47 | }, 48 | "minimum-stability": "dev", 49 | "prefer-stable": true 50 | 51 | } 52 | -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | env('LSS_CACHE_KEY','laravel_site_settings_data'), 6 | 7 | // Set to true if you're using Filament (https://filamentphp.com/) 8 | 'filament' => false, 9 | 10 | ]; -------------------------------------------------------------------------------- /database/migrations/2022_12_11_111047_create_settings_groups_table.php: -------------------------------------------------------------------------------- 1 | id(); 18 | $table->string('slug', 190)->unique(); 19 | $table->string('hint', 190)->nullable(); 20 | $table->timestamps(); 21 | }); 22 | } 23 | 24 | /** 25 | * Reverse the migrations. 26 | * 27 | * @return void 28 | */ 29 | public function down() 30 | { 31 | Schema::dropIfExists('setting_groups'); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /database/migrations/2022_12_11_121047_create_settings_table.php: -------------------------------------------------------------------------------- 1 | id(); 17 | 18 | $table->foreignId('setting_group_id') 19 | ->nullable() 20 | ->constrained() 21 | ->cascadeOnDelete() 22 | ->cascadeOnUpdate(); 23 | 24 | $table->string('slug', 190); 25 | $table->string('hint', 190)->nullable(); 26 | $table->string('value', 190); 27 | $table->timestamps(); 28 | 29 | $table->unique(['setting_group_id', 'slug']); 30 | }); 31 | } 32 | 33 | /** 34 | * Reverse the migrations. 35 | * 36 | * @return void 37 | */ 38 | public function down() 39 | { 40 | Schema::dropIfExists('settings'); 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /database/migrations/2022_12_27_234647_change_value_to_text_in_settings.php: -------------------------------------------------------------------------------- 1 | text('value')->change(); 17 | }); 18 | } 19 | 20 | /** 21 | * Reverse the migrations. 22 | * 23 | * @return void 24 | */ 25 | public function down() 26 | { 27 | Schema::table('settings', function (Blueprint $table) { 28 | $table->string('value', 190)->change(); 29 | }); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /database/migrations/2023_02_15_234647_make_value_nullable_in_settings.php: -------------------------------------------------------------------------------- 1 | text('value')->nullable()->change(); 17 | }); 18 | } 19 | 20 | /** 21 | * Reverse the migrations. 22 | * 23 | * @return void 24 | */ 25 | public function down() 26 | { 27 | Schema::table('settings', function (Blueprint $table) { 28 | $table->text('value')->change(); 29 | }); 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /lang/en/ui.php: -------------------------------------------------------------------------------- 1 | 'Main', 5 | 'group' => 'Group', 6 | 'slug' => 'Slug', 7 | 'hint' => 'Hint', 8 | 'not_used' => 'Not used on the site, only for ease of administration!', 9 | 'value' => 'Value', 10 | 'created_at' => 'Created at', 11 | 'updated_at' => 'Updated at', 12 | 'count_settings' => 'Count settings', 13 | 'settings_groups' => 'Settings groups', 14 | 'settings' => 'Settings' 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/ro/ui.php: -------------------------------------------------------------------------------- 1 | 'Principala', 5 | 'group' => 'Grupul', 6 | 'slug' => 'Slug', 7 | 'hint' => 'Indiciu', 8 | 'not_used' => 'Nu este folosit pe site, doar pentru ușurința de administrare!', 9 | 'value' => 'Valoare', 10 | 'created_at' => 'Creat la', 11 | 'updated_at' => 'Actualizat la', 12 | 'count_settings' => 'Numărul de setări', 13 | 'settings_groups' => 'Grupuri de setări', 14 | 'settings' => 'Setări' 15 | ]; 16 | -------------------------------------------------------------------------------- /lang/ru/ui.php: -------------------------------------------------------------------------------- 1 | 'Главная', 5 | 'group' => 'Группа', 6 | 'slug' => 'Slug', 7 | 'hint' => 'Подсказка', 8 | 'not_used' => 'Не используется на сайте, только для удобства администрирования!', 9 | 'value' => 'Значение', 10 | 'created_at' => 'Создано в', 11 | 'updated_at' => 'Обновлено на', 12 | 'count_settings' => 'Счетчик настроек', 13 | 'settings_groups' => 'Группы настроек', 14 | 'settings' => 'Настройки' 15 | ]; 16 | -------------------------------------------------------------------------------- /src/Events/LSSSettingChangedEvent.php: -------------------------------------------------------------------------------- 1 | schema([ 24 | 25 | TextInput::make('slug') 26 | ->required() 27 | ->unique(ignoreRecord: true), 28 | 29 | TextInput::make('hint') 30 | ->nullable(), 31 | 32 | ]); 33 | } 34 | 35 | public static function table(Table $table): Table 36 | { 37 | return $table 38 | ->columns([ 39 | TextColumn::make('id') 40 | ->sortable(), 41 | TextColumn::make('slug') 42 | ->sortable() 43 | ->color('primary'), 44 | TextColumn::make('hint') 45 | ->sortable() 46 | ->color('secondary'), 47 | TextColumn::make('created_at') 48 | ->sortable() 49 | ->dateTime() 50 | ->color('secondary'), 51 | TextColumn::make('updated_at')->dateTime()->color('secondary') 52 | ->sortable() 53 | ->dateTime() 54 | ->color('secondary'), 55 | ]) 56 | ->filters([ 57 | // 58 | ]) 59 | ->actions([ 60 | Tables\Actions\EditAction::make(), 61 | Tables\Actions\DeleteAction::make(), 62 | ]) 63 | ->bulkActions([ 64 | //Tables\Actions\DeleteBulkAction::make(), 65 | ]); 66 | } 67 | 68 | public static function getRelations(): array 69 | { 70 | return [ 71 | // 72 | ]; 73 | } 74 | 75 | public static function getPages(): array 76 | { 77 | return [ 78 | 'index' => \VI\LaravelSiteSettings\Filament\Resources\SettingGroupResource\Pages\ListSettingGroups::route('/'), 79 | 'create' => \VI\LaravelSiteSettings\Filament\Resources\SettingGroupResource\Pages\CreateSettingGroup::route('/create'), 80 | 'edit' => \VI\LaravelSiteSettings\Filament\Resources\SettingGroupResource\Pages\EditSettingGroup::route('/{record}/edit'), 81 | ]; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/Filament/Resources/SettingGroupResource/Pages/CreateSettingGroup.php: -------------------------------------------------------------------------------- 1 | schema([ 25 | 26 | Select::make('author_id') 27 | ->nullable() 28 | ->relationship('settingGroup', 'slug'), 29 | 30 | TextInput::make('slug') 31 | ->required() 32 | ->unique(ignoreRecord: true), 33 | 34 | TextInput::make('hint') 35 | ->nullable(), 36 | 37 | ]); 38 | } 39 | 40 | public static function table(Table $table): Table 41 | { 42 | return $table 43 | ->columns([ 44 | 45 | TextColumn::make('id') 46 | ->sortable(), 47 | 48 | TextColumn::make('settingGroup.slug') 49 | ->color('primary'), 50 | 51 | TextColumn::make('slug') 52 | ->sortable() 53 | ->color('primary'), 54 | 55 | //TextColumn::make('Usage') 56 | // ->copyable() 57 | // ->color('primary'), 58 | 59 | TextColumn::make('hint') 60 | ->sortable() 61 | ->color('secondary'), 62 | 63 | TextColumn::make('created_at') 64 | ->sortable() 65 | ->dateTime() 66 | ->color('secondary'), 67 | 68 | TextColumn::make('updated_at')->dateTime()->color('secondary') 69 | ->sortable() 70 | ->dateTime() 71 | ->color('secondary'), 72 | ]) 73 | ->filters([ 74 | // 75 | ]) 76 | ->actions([ 77 | Tables\Actions\EditAction::make(), 78 | Tables\Actions\DeleteAction::make(), 79 | ]) 80 | ->bulkActions([ 81 | Tables\Actions\DeleteBulkAction::make(), 82 | ]); 83 | } 84 | 85 | public static function getRelations(): array 86 | { 87 | return [ 88 | // 89 | ]; 90 | } 91 | 92 | public static function getPages(): array 93 | { 94 | return [ 95 | 'index' => \VI\LaravelSiteSettings\Filament\Resources\SettingResource\Pages\ListSettings::route('/'), 96 | 'create' => \VI\LaravelSiteSettings\Filament\Resources\SettingResource\Pages\CreateSetting::route('/create'), 97 | 'edit' => \VI\LaravelSiteSettings\Filament\Resources\SettingResource\Pages\EditSetting::route('/{record}/edit'), 98 | ]; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/Filament/Resources/SettingResource/Pages/CreateSetting.php: -------------------------------------------------------------------------------- 1 | has(config('laravelsitesettings.cache_key'))) { 19 | $this->items = cache()->get(config('laravelsitesettings.cache_key')); 20 | return; 21 | } 22 | 23 | try { 24 | $withGroup = SettingGroup::all() 25 | ->load('settings') 26 | ->keyBy('slug') 27 | ->map(function ($group) { 28 | return $group->settings->pluck('value', 'slug'); 29 | }) 30 | ->toArray(); 31 | $withoutGroup = Setting::whereNull('setting_group_id')->get() 32 | ->pluck('value', 'slug') 33 | ->toArray(); 34 | 35 | $this->items = array_merge($withGroup, $withoutGroup); 36 | } catch (\Throwable $e) { 37 | $this->items = []; 38 | } 39 | 40 | } 41 | 42 | /** 43 | * Determine if the given configuration value exists. 44 | * 45 | * @param string $key 46 | * @return bool 47 | */ 48 | public function has($key): bool 49 | { 50 | return Arr::has($this->items, $key); 51 | } 52 | 53 | /** 54 | * Get the specified configuration value. 55 | * 56 | * @param array|string $key 57 | * @param mixed $default 58 | * @return mixed 59 | */ 60 | public function get($key, $default = null): mixed 61 | { 62 | 63 | return Arr::get($this->items, $key, $default); 64 | } 65 | 66 | /** 67 | * Get all of the configuration items for the application. 68 | * 69 | * @return array 70 | */ 71 | public function all(): array 72 | { 73 | return $this->items; 74 | } 75 | 76 | /** 77 | * Set a given configuration value. 78 | * 79 | * @param array|string $key 80 | * @param mixed $value 81 | * @return void 82 | */ 83 | public function set($key, $value = null): void 84 | { 85 | 86 | $keys = is_array($key) ? $key : [$key => $value]; 87 | 88 | foreach ($keys as $key => $value) { 89 | Arr::set($this->items, $key, $value); 90 | 91 | $this->saveToDB($key, $value); 92 | } 93 | 94 | //dd($key, $value); 95 | } 96 | 97 | /** 98 | * Prepend a value onto an array configuration value. 99 | * 100 | * @param string $key 101 | * @param mixed $value 102 | * @return void 103 | */ 104 | public function prepend($key, $value): void 105 | { 106 | $array = $this->get($key, []); 107 | 108 | array_unshift($array, $value); 109 | 110 | $this->set($key, $array); 111 | } 112 | 113 | /** 114 | * Push a value onto an array configuration value. 115 | * 116 | * @param string $key 117 | * @param mixed $value 118 | * @return void 119 | */ 120 | public function push($key, $value): void 121 | { 122 | $array = $this->get($key, []); 123 | 124 | $array[] = $value; 125 | 126 | $this->set($key, $array); 127 | } 128 | 129 | protected function saveToDB($key, $value) 130 | { 131 | 132 | $keys = explode('.', $key); 133 | 134 | if (count($keys) > 2) { 135 | return; 136 | } 137 | 138 | if (count($keys) == 2) { 139 | $group = array_shift($keys); 140 | $group = SettingGroup::firstOrCreate(['slug' => $group])->id; 141 | } 142 | 143 | Setting::updateOrCreate([ 144 | 'setting_group_id' => $group ?? null, 145 | 'slug' => current($keys), 146 | ], [ 147 | 'value' => $value, 148 | ]); 149 | 150 | } 151 | 152 | } -------------------------------------------------------------------------------- /src/LaravelSiteSettingsProvider.php: -------------------------------------------------------------------------------- 1 | app->bind('settings', function ($app) { 18 | return new LaravelSiteSettings(); 19 | }); 20 | 21 | $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'laravelsitesettings'); 22 | 23 | if (config('laravelsitesettings.filament')) { 24 | $this->app->register(FilamentLaravelSiteSettingsProvider::class); 25 | } 26 | } 27 | 28 | public function boot() 29 | { 30 | 31 | if ($this->app->runningInConsole()) { 32 | 33 | $this->publishes([ 34 | __DIR__ . '/../config/config.php' => config_path('laravelsitesettings.php'), 35 | ], 'config'); 36 | 37 | $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); 38 | 39 | } else { 40 | config(['settings' => app('settings')->all()]); 41 | } 42 | 43 | Blade::directive('settings', function ($expression) { 44 | return ""; 45 | }); 46 | 47 | Setting::observe(LSSObserver::class); 48 | SettingGroup::observe(LSSObserver::class); 49 | 50 | $this->loadTranslationsFrom(__DIR__.'/../lang', 'laravel-site-settings'); 51 | 52 | $this->publishes([ 53 | __DIR__.'/../lang' => $this->app->langPath('vendor/laravel-site-settings'), 54 | ]); 55 | 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/Models/Setting.php: -------------------------------------------------------------------------------- 1 | belongsTo(SettingGroup::class); 31 | } 32 | 33 | public function getNameHumanAttribute(): string 34 | { 35 | $string = $this->slug; 36 | 37 | if (! empty($this->name)) { 38 | return $string.' ('.$this->name.')'; 39 | } 40 | 41 | return $string; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Models/SettingGroup.php: -------------------------------------------------------------------------------- 1 | hasMany(Setting::class); 30 | } 31 | 32 | public function getNameHumanAttribute(): string 33 | { 34 | $string = $this->slug; 35 | 36 | if (! empty($this->name)) { 37 | return $string.' ('.$this->name.')'; 38 | } 39 | 40 | return $string; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/MoonShine/Resources/SettingGroupResource.php: -------------------------------------------------------------------------------- 1 | sortable(), 35 | 36 | Text::make(trans('laravel-site-settings::ui.slug'), 'slug') 37 | ->required() 38 | ->sortable() 39 | ->hint('a-z, 0-9, -, _') 40 | ->showOnExport(), 41 | 42 | Text::make(trans('laravel-site-settings::ui.hint'), 'hint') 43 | ->nullable() 44 | ->sortable() 45 | ->hint(trans('laravel-site-settings::ui.not_used')) 46 | ->showOnExport(), 47 | 48 | NoInput::make(trans('laravel-site-settings::ui.created_at'), 'created_at', 49 | fn(SettingGroup $item)=>$item->created_at->isoFormat('lll')) 50 | ->sortable() 51 | ->hideOnForm() 52 | ->showOnExport(), 53 | 54 | NoInput::make(trans('laravel-site-settings::ui.updated_at'), 'updated_at', 55 | fn(SettingGroup $item)=>$item->updated_at->isoFormat('lll')) 56 | ->sortable() 57 | ->hideOnForm() 58 | ->showOnExport(), 59 | 60 | NoInput::make(trans('laravel-site-settings::ui.count_settings'), 'settings_count') 61 | ->sortable() 62 | ->hideOnForm() 63 | ->hideOnDetail() 64 | ->showOnExport(), 65 | ]), 66 | ]; 67 | } 68 | 69 | public function components(): array 70 | { 71 | return [ 72 | ]; 73 | } 74 | 75 | public function rules($item): array 76 | { 77 | return [ 78 | 'slug' => 'required|max:190|regex:/^([a-z0-9\-\_]+)$/i|unique:setting_groups,slug,' . $item->getKey(), 79 | 'hint' => 'nullable|max:190', 80 | ]; 81 | } 82 | 83 | public function search(): array 84 | { 85 | return ['id', 'slug', 'hint']; 86 | } 87 | 88 | public function filters(): array 89 | { 90 | return [ 91 | ]; 92 | } 93 | 94 | public function actions(): array 95 | { 96 | return [ 97 | 98 | ]; 99 | } 100 | 101 | public function query(): Builder 102 | { 103 | return parent::query()->withCount('settings'); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/MoonShine/Resources/SettingResource.php: -------------------------------------------------------------------------------- 1 | sortable(), 38 | 39 | BelongsTo::make( 40 | trans('laravel-site-settings::ui.group'), 41 | 'settingGroup', 42 | new SettingGroupResource() 43 | ) 44 | ->nullable() 45 | ->sortable() 46 | ->showOnExport(), 47 | 48 | Text::make(trans('laravel-site-settings::ui.slug'), 'slug') 49 | ->required() 50 | ->sortable() 51 | ->hint('a-z, 0-9, -, _') 52 | ->showOnExport(), 53 | 54 | Text::make(trans('laravel-site-settings::ui.hint'), 'hint') 55 | ->nullable() 56 | ->sortable() 57 | ->hint(trans('laravel-site-settings::ui.not_used')) 58 | ->showOnExport(), 59 | 60 | Textarea::make(trans('laravel-site-settings::ui.value'), 'value')->nullable()->sortable() 61 | ->showOnExport(), 62 | 63 | NoInput::make(trans('laravel-site-settings::ui.created_at'), 'created_at', 64 | fn(Setting $item) => $item->created_at->isoFormat('lll')) 65 | ->sortable() 66 | ->hideOnForm() 67 | ->showOnExport(), 68 | 69 | NoInput::make(trans('laravel-site-settings::ui.updated_at'), 'updated_at', 70 | fn(Setting $item) => $item->updated_at->isoFormat('lll')) 71 | ->sortable() 72 | ->hideOnForm() 73 | ->showOnExport(), 74 | 75 | 76 | ]), 77 | ]; 78 | } 79 | 80 | public function components(): array 81 | { 82 | return [ 83 | ]; 84 | } 85 | 86 | public function rules($item): array 87 | { 88 | return [ 89 | 'setting_group_id' => 'nullable|exists:setting_groups,id', 90 | 'slug' => [ 91 | 'required', 92 | 'max:190', 93 | 'regex:/^([a-z0-9\-\_]+)$/i', 94 | 'unique:setting_groups,slug', 95 | Rule::unique('settings', 'slug')->where(function ($query) use ($item) { 96 | return $query->where('id', '!=', $item->getKey()) 97 | ->where('setting_group_id', $item->setting_group_id); 98 | }), 99 | ], 100 | 'hint' => 'nullable|max:190', 101 | 'value' => 'nullable|string', 102 | ]; 103 | } 104 | 105 | public function search(): array 106 | { 107 | return ['id', 'slug', 'hint', 'value']; 108 | } 109 | 110 | public function filters(): array 111 | { 112 | return [ 113 | BelongsToFilter::make('Group', 'settingGroup'), 114 | ]; 115 | } 116 | 117 | public function actions(): array 118 | { 119 | return [ 120 | 121 | ]; 122 | } 123 | 124 | public function query(): Builder 125 | { 126 | return parent::query()->with('settingGroup'); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/Observers/LSSObserver.php: -------------------------------------------------------------------------------- 1 | forget(config('laravelsitesettings.cache_key')); 17 | 18 | if ($model instanceof SettingGroup) { 19 | LSSSettingGroupChangedEvent::dispatch($model); 20 | } elseif ($model instanceof Setting) { 21 | LSSSettingChangedEvent::dispatch($model); 22 | } 23 | } 24 | 25 | 26 | public function deleted(Model $model) 27 | { 28 | cache()->forget(config('laravelsitesettings.cache_key')); 29 | } 30 | 31 | 32 | public function restored(Model $model) 33 | { 34 | cache()->forget(config('laravelsitesettings.cache_key')); 35 | } 36 | 37 | 38 | public function forceDeleted(Model $model) 39 | { 40 | cache()->forget(config('laravelsitesettings.cache_key')); 41 | } 42 | } -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | all(); 18 | } 19 | 20 | if (is_array($key)) { 21 | return app('settings')->set($key); 22 | } 23 | 24 | return app('settings')->get($key, $default); 25 | } 26 | } --------------------------------------------------------------------------------