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();

How to run a VirtualBox Linux nested inside a VirtualBox Windows running on Linux so you can ShareX a Linux desktop

Yes, you could use Wine, but for diehards, here's this. I used Linux Mint and Windows Ameliorated, but the same basic steps should work for other flavors. You're going to want a good amount of RAM, disk space, and a fast CPU. I used 12Gb RAM for the outer VirtualBox and 8Gb RAM for the innermost,  […]

Continue reading

How to fix security issue in Dradis where Ruby Rails Mini Profiler "Rack Environment" is enabled and visible when using pp=env

I'm not familiar with Ruby, Rails, Puma, or anything else in the Ruby environment, although I'm well familiar with other programming environments which are similar. Still, it took me longer to install Dradis than it should have, and I finally gave up on the Docker version entirely. I had to cobble  […]

Continue reading

'The reward of wisdom comes in silence,' saith the fool, and then he spoke no more

To listen the gift of listening, one must first listen What a way to begin the year. This morning I awakened in the dark before the alarm went off, which is always a good way to start a day. Quiet thoughts ambled through my mind for awhile. At the conclusion of a chain of thoughts which have now  […]

Continue reading

Eramba System Health Check fails immediately after installation due to cron error but really it's an .env file issue

System Health Warning: This installation of eramba has a few issues in order to work properly. Check on the settings page under health monitor what could be the issues or click here

The .env file for an eramba installation cannot have #comments. If you have comments, it will silently fail, and you'll see this error message: "System Health Warning: This installation of eramba has a few issues in order to work properly. Check on the settings page under health monitor what  […]

Continue reading

How to open files in vscodium using link in a browser to vscode: add a registry key

To open up a file within a browser having a vscode://file/ URL like this: vscode://file/C:\xampp\htdocs\mycoolapp\app\Models\Membership.php So it looks like this:   Then save the following as a registry file named vscode.reg: Windows Registry Editor Version 5.00  […]

Continue reading

How to fix the multiple monitor resize after lock screen issue in Ubuntu Linux (and Mint, PopOS, etc) with GNOME

TL;DR Quick Fix is at the END for people who want to get to the deets w/o prolegomenon. I put as many keywords as I could into the headline so this post is easy for others to find. Although I'm not a gamer, I have a super duper Lenovo Legion gamer laptop with all kinds of nVidia power for my daily  […]

Continue reading

Euclid's clever solution to the problems of the void and the infinitesimal

Euclid's Definition 1 Now it's time to unravel something even more fundamental than the 5th postulate: the 1st definition. We normally think of a geometrical point as being smaller than anything, because of where Euclid placed it in the hierarchy of geometry. Building up from simple to more complex,  […]

Continue reading

A way of understanding true trinary logic which includes the continuum and reveals the importance of the word AND

Introducing true trinary Trinary logic is introduced at Wikipedia like this: In logic, a three-valued logic (also trinary logic, trivalent, ternary, or trilean, sometimes abbreviated 3VL) is any of several many-valued logic systems in which there are three truth values indicating true, false, and  […]

Continue reading

An intriguing argument that one plus one is equal to three

No, one times one does not equal two. But here is an intriguing argument that one plus one is equal to three. It sounds absurd, so let's start small and carefully establish the logical elements which comprise this argument. Each element will be seen to be logically sound in themselves. Then we'll  […]

Continue reading

Thinking about a rare opposite of Schadenfreude which is neither Freudefreude nor Mudita but Schadenmitgefühl

Schadenfreude: it's not just for humans

The wild headline is intentional because the audience for this article will most likely arrive here by search engines, as people are trying to find "the opposite of schadenfreude," but not the obvious opposite. Of these visitors only a few will appreciate this little word adventure here,  […]

Continue reading

Allow the Deep to Speak

This article is going to be stored in a database with a record ID of 281, which is a prime number, as soon as I click "Save." At the moment that I write these introductory words, it exists in an aether of nowhere. Neither here nor there. It has no record ID. It is thus a virtual weblog  […]

Continue reading

What to do when Roundcube says: Decryption failed: Key password required, and: Could not decrypt this message

Roundcube error: Decryption failed: Key password required.

Roundcube is great. Easy to get up and running, but it surprises me that I couldn't find the following solution online, so I'm making a post here. When you receive an email (say, from a public key generator) that is encrypted by your own PRIVATE key and you haven't fully configured encryption, it  […]

Continue reading

A whole new way into heart meditation reveals... light... on Thanksgiving morning

A year of mindful[1] meditation Here it is early the morning of Thanksgiving 2023 and I just discovered a rather deep heartful meditation which took about a year to learn. The essential idea is quite simple and took only a few minutes to comprehend, but it took a year of preparation so that I could  […]

Continue reading

"I want to break this apart and put it back together again" -- while trying to solve the Rubik's cube

Rubik's cube

This is a weblog post about what makes a hacker, and why there are "good" hackers and "bad" hackers (which is about quality, not whitehat vs blackhat), an insight I discovered while in a brief conversation with my 10-year-old daughter. My daughter has discovered the Rubik's cube,  […]

Continue reading

Page top