F
Filament5mo ago
nowak

Customize file upload processing

I am trying to use my own service class method for processing and storing images on my resource, as I am using laravel + inertia + vue3 offering the same CRUD operations for one of my resources. Since I am already using my own service class for image file processing and storage, I would like to reuse this logic when uploading image files through Filament. Is it possible to parse the uploaded file to a defined method, something like this:
FileUpload::make('picture')
->image()
->maxSize(5120)
->saveUploadedFileUsing(function (FileUpload $field, $file, $record) {
return GroupService::processAndStoreGroupPicture($file, $record->name);
FileUpload::make('picture')
->image()
->maxSize(5120)
->saveUploadedFileUsing(function (FileUpload $field, $file, $record) {
return GroupService::processAndStoreGroupPicture($file, $record->name);
Or what would be the most appropriate way of doing this with Filament?
Solution:
Did you figure this out? Running into the same challenge.
Jump to solution
3 Replies
Solution
Jon Mason
Jon Mason3mo ago
Did you figure this out? Running into the same challenge.
nowak
nowak3mo ago
Yes I processed the added image in the Create page by using the afterCreate lifecycle hook:
<?php

namespace App\Filament\Resources\GroupResource\Pages;

use App\Filament\Resources\GroupResource;
use Filament\Resources\Pages\CreateRecord;
use App\Models\Group;
use App\Services\GroupService;
use Illuminate\Support\Str;

use Illuminate\Support\Facades\Log;

class CreateGroup extends CreateRecord
{
protected static string $resource = GroupResource::class;

protected function afterCreate(): void
{
$group = $this->record; // The newly created group instance

// Process the uploaded file if it exists
if ($group->picture_path) {
$newUrl = GroupService::optimizeAndRenameImage($group->picture_path, $group->name, $group->id);

// Update the group with the new picture URL
$group->picture_path = $newUrl;

$group->save();

// Log the new picture URL (for debugging)
Log::info('Group picture processed', ['group_id' => $group->id, 'picture_path' => $group->picture_path]);
}
}
}
<?php

namespace App\Filament\Resources\GroupResource\Pages;

use App\Filament\Resources\GroupResource;
use Filament\Resources\Pages\CreateRecord;
use App\Models\Group;
use App\Services\GroupService;
use Illuminate\Support\Str;

use Illuminate\Support\Facades\Log;

class CreateGroup extends CreateRecord
{
protected static string $resource = GroupResource::class;

protected function afterCreate(): void
{
$group = $this->record; // The newly created group instance

// Process the uploaded file if it exists
if ($group->picture_path) {
$newUrl = GroupService::optimizeAndRenameImage($group->picture_path, $group->name, $group->id);

// Update the group with the new picture URL
$group->picture_path = $newUrl;

$group->save();

// Log the new picture URL (for debugging)
Log::info('Group picture processed', ['group_id' => $group->id, 'picture_path' => $group->picture_path]);
}
}
}
I kept my Fileupload field simple:
FileUpload::make('picture_path')
->label('Dropoff picture')
->directory('group-pictures')
->image(),

FileUpload::make('picture_path')
->label('Dropoff picture')
->directory('group-pictures')
->image(),

Jon Mason
Jon Mason3mo ago
ok, thanks for that!