F
Filament16mo ago
toeknee

S3 File Uploads

Guys, what am I missing? My S3 storage driver works fine in laravel, but when using FileUpload as per:
FileUpload::make('cv')
->label('CV')
->enableDownload()
->disk('s3')
->directory($dir)
->required()
->disablePreview()
->columnSpan(6),
FileUpload::make('cv')
->label('CV')
->enableDownload()
->disk('s3')
->directory($dir)
->required()
->disablePreview()
->columnSpan(6),
Nothing is uploaded to S3 and instead stays in the Livewire Tmp Dir. Why...
Solution:
make sure youre using getState() and not manually accessing the data from Livewire
Jump to solution
10 Replies
Dennis Koch
Dennis Koch16mo ago
Also after saving? Did you try removing directory()?
toeknee
toekneeOP16mo ago
After Saving we get a data array of:
"documents": {
"cv": {
"c24fde3e-708c-49bc-b261-ad7de8545225": {
"disk": "local"
}
},
}
"documents": {
"cv": {
"c24fde3e-708c-49bc-b261-ad7de8545225": {
"disk": "local"
}
},
}
Tried without directory and no different
Dennis Koch
Dennis Koch16mo ago
That's weird. Why is the disk even in there?
toeknee
toekneeOP16mo ago
I suspect it's becasue I'm not casting to array on the model. But I can't as it's a form build and one level deep i.e. ["data"]["documents"]["cv"] Starting to think I need to write my own save function for file uploads. which isn't a huge deal, but thought that was why we use Filepond to handle all that and just return the path for where it has been uploaded too? I am on a custom livewire component, so looks like I'll just look to handle the saving myself I suppose This works
if(array_key_exists('documents', $this->data)) {
foreach ($this->data['documents'] as $key => $document) {
// Clear the files array to convert to array of filenames
unset($this->data['documents'][$key]['file']);

// Loop through as we are an array
foreach ($document as $doc) {

// Set the store as
$doc->storeAs('system/' . \Str::slug($instance->team->name) . '/files/' . $instance_id,
$doc->getClientOriginalName(), 's3');
$file = $doc->store();
// set the file name into the files array
$this->data['documents'][$key]['file'][] = $file;
}
}
}
if(array_key_exists('documents', $this->data)) {
foreach ($this->data['documents'] as $key => $document) {
// Clear the files array to convert to array of filenames
unset($this->data['documents'][$key]['file']);

// Loop through as we are an array
foreach ($document as $doc) {

// Set the store as
$doc->storeAs('system/' . \Str::slug($instance->team->name) . '/files/' . $instance_id,
$doc->getClientOriginalName(), 's3');
$file = $doc->store();
// set the file name into the files array
$this->data['documents'][$key]['file'][] = $file;
}
}
}
But seems far to hacky @Dan Harrin sorry for the tag.. but running out of ideas and docs are slim on this one :/... Is the fileupload in a custom livewire component requiring us to manually save? Or implement an additional trait? Or do we need to run a custom function which is monitored to handle the file uploads?
Dan Harrin
Dan Harrin16mo ago
nope it should not require you to manually save
Solution
Dan Harrin
Dan Harrin16mo ago
make sure youre using getState() and not manually accessing the data from Livewire
Dan Harrin
Dan Harrin16mo ago
getState() is the thing that validates and stores files and saves relationships
toeknee
toekneeOP16mo ago
This is likely why! Thanks Dan, will double check it. I think I maybe using raw data from when I was storing for a resume function later
Dan Harrin
Dan Harrin16mo ago
please follow the "adding a form to a livewire component" docs ;)
toeknee
toekneeOP16mo ago
Bahaha tbf I added this ages ago, it’s just a slip of duplicating a function. Sorry Dan 🙈 Yep just remoted in and checked... I was validating it but then just returning $this->data :/ ffs. $this->getState does render the correct links etc Thanks a Bunch Dan, only wasted 3/4 of a day on it haha
Want results from more Discord servers?
Add your server