F
Filament16mo ago
jsn1nj4

Change in `afterStateUpdated()` hook for file uploads?

I have an Image model I'm saving extra data for on upload. In v2, I got this working using the afterStateUpdated() hook: https://github.com/JSn1nj4/ElliotDerhay.com/blob/feature/125-dashboard-overhaul-with-filament/app/Filament/Resources/ImageResource/Pages/CreateImage.php#L27 Reading this other help thread is what got me there: https://discord.com/channels/883083792112300104/1130866560848892015 This worked out so well because the second param was a TemporaryUploadedFile. It had everything I needed to fill the other fields and locate the file. Now I'm v3, that second param is just a filename string and I'm having difficulty locating the actual file. What's the best way to get file info for uploads now?
GitHub
ElliotDerhay.com/app/Filament/Resources/ImageResource/Pages/CreateI...
My personal website project. Contribute to JSn1nj4/ElliotDerhay.com development by creating an account on GitHub.
6 Replies
awcodes
awcodes16mo ago
hmmm. I just ran a test on the demo app and $state is returning a TemporaryUploadedFile for me.
awcodes
awcodes16mo ago
->afterStateUpdated(fn ($state) => dd($state))
jsn1nj4
jsn1nj4OP16mo ago
Really, with Filament v3? Is it using Livewire V3 under the hood as well or v2?
jsn1nj4
jsn1nj4OP16mo ago
If you mean the one from this page, I'll give it a shot and see what I might be missing. Thanks! https://filamentphp.com/docs/3.x/panels/getting-started#the-demo-project
awcodes
awcodes16mo ago
yea, v3 Filament v3 uses LW3, yes.
jsn1nj4
jsn1nj4OP16mo ago
@awcodes seems you're right, it's doing it. Something funky must've been happening when I was trying to organize the snippet below in an instance method. I've decided to just use a plain closure instead of handing off to a method.
->afterStateUpdated(static function (Forms\Set $set, TemporaryUploadedFile $state) {
$dto = ImageDTO::fromUpload($state);

$set('name', $dto->name);
$set('collection', $dto->collection);
$set('disk', $dto->disk);
$set('file_name', $dto->file_name);
$set('mime_type', $dto->mime_type);
$set('path', $dto->path);
$set('size', $dto->size);
$set('file_hash', $dto->file_hash);
})
->afterStateUpdated(static function (Forms\Set $set, TemporaryUploadedFile $state) {
$dto = ImageDTO::fromUpload($state);

$set('name', $dto->name);
$set('collection', $dto->collection);
$set('disk', $dto->disk);
$set('file_name', $dto->file_name);
$set('mime_type', $dto->mime_type);
$set('path', $dto->path);
$set('size', $dto->size);
$set('file_hash', $dto->file_hash);
})
Also had to manually set up state fields as described here: https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#saving-form-data-to-individual-properties Now have to figure out why my form fields are apparently not mapping to the model, but this is much bigger progress than I made before. Thanks!
Want results from more Discord servers?
Add your server