Skip to content

Instantly share code, notes, and snippets.

@pekka
Last active October 17, 2023 09:15
Show Gist options
  • Save pekka/c9c9503b1ddd2c4ed6b762711d2e59af to your computer and use it in GitHub Desktop.
Save pekka/c9c9503b1ddd2c4ed6b762711d2e59af to your computer and use it in GitHub Desktop.
Addition to Laravel Daily's mini tutorial "Filament & Laravel: Delete Unused Files if Model is Updated/Deleted" - a "saved()" method that can handle the image field set to multiple
public function saved(Task $task): void
{
if ($task->isDirty('attachment')) {
$originalFieldContents = $task->getOriginal('attachment');
$newFieldContents = $task->attachment;
# We attempt to JSON decode the field. If it is an array, this is an indication we have ->multiple() activated
$originalFieldContentsDecoded = json_decode($task->getOriginal('attachment'));
# Clean up empty entries in the resulting array
if (is_array($originalFieldContentsDecoded)) $originalFieldContentsDecoded = array_filter($originalFieldContentsDecoded);
# Simple case: one file
if (!is_array($originalFieldContentsDecoded) or count($originalFieldContentsDecoded) == 0)
{
Storage::disk('public')->delete($originalFieldContents);
}
# Complex case: multiple files
else
{
foreach ($originalFieldContentsDecoded as $originalFile)
{
if (trim($originalFile) != null && !in_array($originalFile, $newFieldContents))
{
Storage::disk('public')->delete($originalFile);
}
}
}
}
}
public function deleted(Task $task): void
{
if (! is_null($task->attachment)) {
# We attempt to JSON decode the field. If it is an array, there are multiple files
$fieldContentsDecoded = json_decode($task->attachment);
# Simple case: one file
if (!is_array($fieldContentsDecoded))
{
Storage::disk('public')->delete($task->attachment);
}
# Complex case: multiple files
else
{
foreach ($fieldContentsDecoded as $file)
{
Storage::disk('public')->delete($file);
}
}
}
}
@justgkp
Copy link

justgkp commented Aug 10, 2023

i updated the method and now it's woking for me otherwise i was getting error "json_decode(): Argument #1 ($json) must be of type string, array given"

        if (is_array($originalFieldContents)) {
            $originalFieldContentsDecoded = $originalFieldContents;
        } else {
            $originalFieldContentsDecoded = json_decode($originalFieldContents);
        }

@uarefans
Copy link

i updated the method and now it's woking for me otherwise i was getting error "json_decode(): Argument #1 ($json) must be of type string, array given"

        if (is_array($originalFieldContents)) {
            $originalFieldContentsDecoded = $originalFieldContents;
        } else {
            $originalFieldContentsDecoded = json_decode($originalFieldContents);
        }

thank you, work like a charm

change line 10

           $originalFieldContentsDecoded = json_decode($task->getOriginal('attachment'));

with

        if (is_array($originalFieldContents)) {
            $originalFieldContentsDecoded = $originalFieldContents;
        } else {
            $originalFieldContentsDecoded = json_decode($originalFieldContents);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment