where('attachmentable_type', $classModel) ->first(); if ($existingAttachment) { $oldFilePath = $path.'/'.$existingAttachment->created_at->format('Y-m-d').'/'.$existingAttachment->name.'.'.$existingAttachment->extension; Storage::disk('public')->delete($oldFilePath); $existingAttachment->delete(); } $date = now()->format('Y-m-d'); $extension = $file->getClientOriginalExtension(); $fileName = Str::uuid(); $file->storeAs($path.'/'.$date, $fileName.'.'.$extension, 'public'); Attachment::create([ 'attachmentable_id' => $featureId, 'attachmentable_type' => $classModel, 'name' => $fileName, 'extension' => $extension, 'size' => $file->getSize(), ]); }); } }