diff --git a/app/Http/Controllers/AssetPhotoController.php b/app/Http/Controllers/AssetPhotoController.php new file mode 100644 index 0000000..9092c8b --- /dev/null +++ b/app/Http/Controllers/AssetPhotoController.php @@ -0,0 +1,52 @@ +validate([ + 'photos' => ['required', 'array'], + 'photos.*' => ['image', 'mimes:jpeg,png,jpg,webp', 'max:4096'], + ]); + + $stored = 0; + + foreach ($data['photos'] as $file) { + $path = $file->store("assets/{$asset->id}", 'public'); + + AssetPhoto::create([ + 'asset_id' => $asset->id, + 'path' => $path, + 'is_primary' => $asset->photos()->count() === 0, + ]); + $stored++; + } + + return back()->with('success', "{$stored} foto aset berhasil diunggah."); + } + + public function destroy(AssetPhoto $asset_photo): RedirectResponse + { + Storage::disk('public')->delete($asset_photo->path); + $asset = $asset_photo->asset; + $wasPrimary = $asset_photo->is_primary; + $asset_photo->delete(); + + if ($wasPrimary) { + $next = $asset->photos()->first(); + if ($next) { + $next->update(['is_primary' => true]); + } + } + + return back()->with('success', 'Foto aset berhasil dihapus.'); + } +} diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 097fc5e..74f6b61 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -107,4 +107,14 @@ class Asset extends Model { return $this->hasMany(AssetMaintenance::class); } + + public function photos() + { + return $this->hasMany(AssetPhoto::class); + } + + public function primaryPhoto() + { + return $this->hasOne(AssetPhoto::class)->where('is_primary', true); + } } diff --git a/app/Models/AssetPhoto.php b/app/Models/AssetPhoto.php new file mode 100644 index 0000000..e6460a5 --- /dev/null +++ b/app/Models/AssetPhoto.php @@ -0,0 +1,28 @@ + 'boolean', + ]; + + public function asset() + { + return $this->belongsTo(Asset::class); + } +} diff --git a/database/migrations/2025_12_08_000000_create_asset_photos_table.php b/database/migrations/2025_12_08_000000_create_asset_photos_table.php new file mode 100644 index 0000000..ba6f5d3 --- /dev/null +++ b/database/migrations/2025_12_08_000000_create_asset_photos_table.php @@ -0,0 +1,25 @@ +uuid('id')->primary(); + $table->foreignUuid('asset_id')->constrained('assets')->cascadeOnDelete(); + $table->string('path'); + $table->boolean('is_primary')->default(false); + $table->string('caption')->nullable(); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('asset_photos'); + } +}; diff --git a/resources/views/assets/show.blade.php b/resources/views/assets/show.blade.php index 7c385b3..e5e5acd 100644 --- a/resources/views/assets/show.blade.php +++ b/resources/views/assets/show.blade.php @@ -45,16 +45,35 @@
QR belum tersedia.
- @endif -{{ route('assets.show', $asset) }}
QR belum tersedia.
+ @endif +{{ route('assets.show', $asset) }}