Add asset photo uploads with gallery and QR section
This commit is contained in:
52
app/Http/Controllers/AssetPhotoController.php
Normal file
52
app/Http/Controllers/AssetPhotoController.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Asset;
|
||||
use App\Models\AssetPhoto;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class AssetPhotoController extends Controller
|
||||
{
|
||||
public function store(Request $request, Asset $asset): RedirectResponse
|
||||
{
|
||||
$data = $request->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.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user