From 9c5a29d933da1a64d92676cc17227784bce4b7ce Mon Sep 17 00:00:00 2001 From: ncoronel Date: Thu, 25 Jun 2026 14:31:03 -0300 Subject: [PATCH] feat(attachments): add 'key' field to Attachment model and migration, generate UUID on creation --- app/Domains/Attachable/Models/Attachment.php | 8 ++++++++ app/Domains/StorageTest/Services/S3TestService.php | 2 ++ .../2026_06_25_000500_create_attachments_table.php | 1 + 3 files changed, 11 insertions(+) diff --git a/app/Domains/Attachable/Models/Attachment.php b/app/Domains/Attachable/Models/Attachment.php index 9f18949..ef5e514 100644 --- a/app/Domains/Attachable/Models/Attachment.php +++ b/app/Domains/Attachable/Models/Attachment.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Attributes\Fillable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Support\Str; #[Fillable([ 'attachable_type', @@ -23,6 +24,13 @@ class Attachment extends Model protected $table = 'attachments'; + protected static function booted(): void + { + static::creating(function (self $attachment): void { + $attachment->key = (string) Str::uuid(); + }); + } + protected function casts(): array { return [ diff --git a/app/Domains/StorageTest/Services/S3TestService.php b/app/Domains/StorageTest/Services/S3TestService.php index 3638ced..5243a85 100644 --- a/app/Domains/StorageTest/Services/S3TestService.php +++ b/app/Domains/StorageTest/Services/S3TestService.php @@ -37,6 +37,7 @@ class S3TestService return [ 'disk' => 's3', 'directory' => $directory, + 'key' => $path, 'path' => $path, 'filename' => basename($path), 'original_name' => $file->getClientOriginalName(), @@ -55,6 +56,7 @@ class S3TestService { return [ 'disk' => 's3', + 'key' => $path, 'path' => $path, 'temporary_url' => $this->temporaryUrlForPath($path, $expiresInMinutes), 'temporary_url_expires_at' => now()->addMinutes($expiresInMinutes)->toIso8601String(), diff --git a/database/migrations/2026_06_25_000500_create_attachments_table.php b/database/migrations/2026_06_25_000500_create_attachments_table.php index 79c7784..30543d2 100644 --- a/database/migrations/2026_06_25_000500_create_attachments_table.php +++ b/database/migrations/2026_06_25_000500_create_attachments_table.php @@ -15,6 +15,7 @@ return new class extends Migration $table->id(); $table->string('attachable_type'); $table->unsignedBigInteger('attachable_id'); + $table->uuid('key')->unique(); $table->string('path'); $table->string('filename'); $table->string('type')->nullable();