Skip to content

Instantly share code, notes, and snippets.

@pekka
pekka / TaskObserver.php
Last active October 17, 2023 09:15
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'));