Get filename and path after a file is uploaded using Laravel Filament afterStateUpdated

It should not be so difficult to find something so trivial. So in hopes of helping someone else find the answer more quickly than the several days it took me, here is a conversation I had recently in a Discord chat for Laravel Filament. Scroll to the very end for the TL;DR one-liner answer.

===============me:

I'd like to get the filename and path after a file is uploaded using afterStateUpdated. After the file is uploaded, but before the (create) record is saved, I can see the file in the livewire-tmp folder, but how do I get to it programmatically? (I want to convert a PDF to text at this point)

I've searched diligently for code examples on accessing file upload details using $get or $component in an afterStateUpdated handler, but getting nowhere. Seems like something that should be easier to find. Please advise.

===============answer1:

I think you can use livewire?

->afterStateUpdated(fn($get, $livewire) => dd($livewire))

But also see: https://www.answeroverflow.com/m/1170008418648592434

===============me:

$livewire works as suggested. It appears to have roughly the same information available as $component. However, what I'm asking for is how to get to the uploaded filename. dd shows me the filename, buried in the data array, but how do I access that programmatically?

The linked answeroverflow answer uses $this->form->getRawState() which throws an error if I try to use it inside the resource class, so I don't even know how to use it.

===============answer2:

try with $livewire->data

===============me:

Cool. We're one step closer. How do I programmatically access the 'path' field in the enclosed 'data' array? $livewire->data['existing_policy']->???

<Screenshot>

===============answer3:

the answer is in your screenshot, that's written "array", so something like:

$livewire->data['existing_policy'][0]['path']

===============me:

I tried that early on and [0] throws an error ("Undefined array key 0"). I tried numerous variations of brackets and arrows and none of them work. I've tried casting the object as an object and as an array (which is technically possible) in order to access what's inside of it. If I call the array with the GUID $livewire->data['23a1df3...']

I can get to the object directly, but ['path'] doesn't work. So I tried reset() to get at the zeroth element of the associative array, which worked, but again ['path'] doesn't work. So I tried ->getPath() and all I got is the part I already know: C:\xampp\htdocs\<projectname>\storage\app\livewire-tmp, the path up to but not including the filename of the uploaded file. What am I missing?

[Update]: Found it. I was missing getFilename(). This works:

$temporaryuploadedfileobject = $livewire->data['existing_policy'];
$filename = reset($temporaryuploadedfileobject)->getFilename();
"EsyyO09GAzYENzZyhSjpfMlJLV28iV-metaQXV0b2RpZGFjdGljaXNtMiAtIFdpa2lwZWRpYS5wZGY=-.pdf"

So the one-liner answer is this:

$filename = reset($livewire->data['myfieldname'])->getFilename();

Add a comment

Fields followed by * are mandatory

HTML code is displayed as text and web addresses are automatically converted.

Page top