How to fix Voyager file uploading error?
To fix this error, you have to modify a method provided by the "VoyagerBaseController".
Step 1: Locate "VoyagerBaseController.php". It can be found here:
vendor\tcg\voyager\src\Http\Controllers\VoyagerBaseController.php
Step 2: Locate the method named "cleanup".
Step 3: Inside the "cleanup" method, you will find this code with a comment:
// Delete Files
foreach ($dataType->deleteRows->where('type', 'file') as $row) {
if (isset($data->{$row->field})) {
foreach (json_decode($data->{$row->field}) as $file) {
$this->deleteFileIfExists($file->download_link);
}
}
}
Step 4: Now, you have to remove or comment out this entire foreach loop code because this code is where data deletion fails.
Step 5: After removing or commenting out the previous delete code, copy and paste this new code. This new code has better handling of deleting single files and multiple files simultaneously."
//Delete single and multipal files
foreach ($dataType->deleteRows->where('type', 'file') as $row) {
if (isset($data->{$row->field})) {
$fileData = $data->{$row->field};
if (!empty($fileData)) {
$decodedData = json_decode($fileData);
if (json_last_error() === JSON_ERROR_NONE && $decodedData !== null) {
if (is_array($decodedData)) {
foreach ($decodedData as $file) {
if (isset($file->download_link)) {
Storage::disk('public')->delete($file->download_link);
}
}
} else {
if (isset($decodedData->download_link)) {
Storage::disk('public')->delete($decodedData->download_link);
}
}
} else {
try {
if (Storage::disk('public')->exists($fileData)) {
Storage::disk('public')->delete($fileData);
}
} catch (\Exception $e) {
Log::error('Exception occurred while deleting file: ' . $e->getMessage());
}
}
}
}
}
No comments:
Post a Comment