ruff
ruff
FFilament
Created by ruff on 3/20/2024 in #❓┊help
Empty string to null disable
Nullable won't solve the problem, cause i need exactly opposite
5 replies
FFilament
Created by ruff on 1/28/2024 in #❓┊help
FileUpload store path instead of filename
I was able to achieve this by extending FileUpload class
namespace App\Forms\Components;

class FileUpload extends BaseFileUpload
{
protected function setUp(): void
{
parent::setUp();

$this->afterStateHydrated(static function (BaseFileUpload $component, string | array | null $state): void {
if (blank($state)) {
$component->state([]);

return;
}

$shouldFetchFileInformation = $component->shouldFetchFileInformation();

$files = collect(Arr::wrap($state))
->map(static fn (string $file): string => str_replace("uploads/", "", $file))
->filter(static function (string $file) use ($component, $shouldFetchFileInformation): bool {
if (blank($file)) {
return false;
}

if (! $shouldFetchFileInformation) {
return true;
}

try {
return $component->getDisk()->exists($file);
} catch (UnableToCheckFileExistence $exception) {
return false;
}
})
->mapWithKeys(static fn (string $file): array => [((string) Str::uuid()) => $file])
->all();

$component->state($files);
});

$this->dehydrateStateUsing(static function (BaseFileUpload $component, ?array $state): string | array | null | TemporaryUploadedFile {
$files = collect(array_values($state ?? []))->map(static fn (string $file): string => "uploads/".($file));

if ($component->isMultiple()) {
return $files;
}

return $files[0] ?? null;
});
}
}
namespace App\Forms\Components;

class FileUpload extends BaseFileUpload
{
protected function setUp(): void
{
parent::setUp();

$this->afterStateHydrated(static function (BaseFileUpload $component, string | array | null $state): void {
if (blank($state)) {
$component->state([]);

return;
}

$shouldFetchFileInformation = $component->shouldFetchFileInformation();

$files = collect(Arr::wrap($state))
->map(static fn (string $file): string => str_replace("uploads/", "", $file))
->filter(static function (string $file) use ($component, $shouldFetchFileInformation): bool {
if (blank($file)) {
return false;
}

if (! $shouldFetchFileInformation) {
return true;
}

try {
return $component->getDisk()->exists($file);
} catch (UnableToCheckFileExistence $exception) {
return false;
}
})
->mapWithKeys(static fn (string $file): array => [((string) Str::uuid()) => $file])
->all();

$component->state($files);
});

$this->dehydrateStateUsing(static function (BaseFileUpload $component, ?array $state): string | array | null | TemporaryUploadedFile {
$files = collect(array_values($state ?? []))->map(static fn (string $file): string => "uploads/".($file));

if ($component->isMultiple()) {
return $files;
}

return $files[0] ?? null;
});
}
}
14 replies
FFilament
Created by ruff on 1/28/2024 in #❓┊help
FileUpload store path instead of filename
It works with TextInput, but gives errors with FileUpload, even if i do no processing at all
->formatStateUsing(fn ($state) => $state)
->formatStateUsing(fn ($state) => $state)
14 replies
FFilament
Created by ruff on 1/28/2024 in #❓┊help
FileUpload store path instead of filename
i need something like this, but i'm not sure if it is possible with FIleUpload field
FileUpload::make('photo')
->image()
->label('Photo')
->formatStateUsing(fn ($state) => str_replace("uploads/", "", $state))
->dehydrateStateUsing(fn (string $state): string => "uploads/".$state)
->required(),
FileUpload::make('photo')
->image()
->label('Photo')
->formatStateUsing(fn ($state) => str_replace("uploads/", "", $state))
->dehydrateStateUsing(fn (string $state): string => "uploads/".$state)
->required(),
14 replies
FFilament
Created by ruff on 1/28/2024 in #❓┊help
FileUpload store path instead of filename
nah, i configured uploads disk, i need path to contain disk's directory
14 replies
FFilament
Created by ruff on 1/28/2024 in #❓┊help
FileUpload store path instead of filename
Let's say i have field called image. Its value is stored in database as 'uploads/somefile.jpg'. FileUpload expects its value to be 'somefile.jpg' in order to work correctly. So i need somehow to alter its value and remove 'uploads/' before it goes to FileUpload, then i need to add it back before it goes to database.
14 replies