Add file type support in settings: Updated SettingForm to include a file upload field for settings, modified the Setting model to handle file values, and added a migration to accommodate the new file type. Enhanced localization files to support the new file type label in both English and Turkish.

This commit is contained in:
Ümit Tunç
2025-10-23 15:21:19 -03:00
parent 1bb06c7bd4
commit 5157fe46ba
5 changed files with 61 additions and 2 deletions
@@ -6,6 +6,7 @@ use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea; use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput; use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle; use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\FileUpload;
use Filament\Schemas\Components\Section; use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Utilities\Get; use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set; use Filament\Schemas\Components\Utilities\Set;
@@ -54,6 +55,7 @@ class SettingForm
'float' => __('settings.type_float'), 'float' => __('settings.type_float'),
'array' => __('settings.type_array'), 'array' => __('settings.type_array'),
'json' => __('settings.type_json'), 'json' => __('settings.type_json'),
'file' => __('settings.type_file'),
]) ])
->live() ->live()
->afterStateUpdated(fn (Set $set) => $set('value', null)), ->afterStateUpdated(fn (Set $set) => $set('value', null)),
@@ -78,10 +80,10 @@ class SettingForm
->label(__('settings.value')) ->label(__('settings.value'))
->helperText(__('settings.value_helper')) ->helperText(__('settings.value_helper'))
->required() ->required()
->visible(fn (Get $get) => !in_array($get('type'), ['boolean'])) ->visible(fn (Get $get) => !in_array($get('type'), ['boolean', 'file']))
->rows(fn (Get $get) => $get('type') === 'text' ? 5 : 3) ->rows(fn (Get $get) => $get('type') === 'text' ? 5 : 3)
->formatStateUsing(function ($state, $record) { ->formatStateUsing(function ($state, $record) {
if ($record && $record->type !== 'boolean') { if ($record && $record->type !== 'boolean' && $record->type !== 'file') {
return $record->value; return $record->value;
} }
return $state; return $state;
@@ -101,6 +103,23 @@ class SettingForm
}) })
->columnSpanFull(), ->columnSpanFull(),
FileUpload::make('value_file')
->label(__('settings.value'))
->helperText(__('settings.value_helper'))
->required()
->visible(fn (Get $get) => $get('type') === 'file')
->disk('public')
->directory('settings')
->acceptedFileTypes(['image/*', 'application/pdf', 'text/*'])
->maxSize(10240) // 10MB
->formatStateUsing(function ($state, $record) {
if ($record && $record->type === 'file') {
return $record->value;
}
return $state;
})
->columnSpanFull(),
Toggle::make('is_active') Toggle::make('is_active')
->label(__('settings.is_active')) ->label(__('settings.is_active'))
->helperText(__('settings.is_active_helper')) ->helperText(__('settings.is_active_helper'))
+10
View File
@@ -15,6 +15,7 @@ class Setting extends Model
'value', 'value',
'value_text', 'value_text',
'value_boolean', 'value_boolean',
'value_file',
'type', 'type',
'group', 'group',
'label', 'label',
@@ -50,6 +51,14 @@ class Setting extends Model
$this->attributes['value'] = $value ? '1' : '0'; $this->attributes['value'] = $value ? '1' : '0';
} }
/**
* Set value from different field names
*/
public function setValueFileAttribute($value)
{
$this->attributes['value'] = $value;
}
/** /**
* Get setting by key * Get setting by key
*/ */
@@ -91,6 +100,7 @@ class Setting extends Model
'integer' => (int) $value, 'integer' => (int) $value,
'float' => (float) $value, 'float' => (float) $value,
'array', 'json' => json_decode($value, true), 'array', 'json' => json_decode($value, true),
'file' => $value, // File path as string
default => $value, default => $value,
}; };
} }
@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->enum('type', ['string', 'text', 'boolean', 'integer', 'float', 'array', 'json', 'file'])->default('string')->change();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->enum('type', ['string', 'text', 'boolean', 'integer', 'float', 'array', 'json'])->default('string')->change();
});
}
};
+1
View File
@@ -59,6 +59,7 @@ return [
'type_float' => 'Float', 'type_float' => 'Float',
'type_array' => 'Array', 'type_array' => 'Array',
'type_json' => 'JSON', 'type_json' => 'JSON',
'type_file' => 'File',
// Groups // Groups
'group_general' => 'General', 'group_general' => 'General',
+1
View File
@@ -59,6 +59,7 @@ return [
'type_float' => 'Ondalıklı Sayı', 'type_float' => 'Ondalıklı Sayı',
'type_array' => 'Dizi', 'type_array' => 'Dizi',
'type_json' => 'JSON', 'type_json' => 'JSON',
'type_file' => 'Dosya',
// Groups // Groups
'group_general' => 'Genel', 'group_general' => 'Genel',