feat: product screenshots gallery

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Doar Mihai
2026-10-17 16:00:38 +03:00
co-authored by Claude Sonnet 5.5
parent bf365d029f
commit a3894d2f37
9 changed files with 314 additions and 2 deletions
+26 -1
View File
@@ -223,6 +223,10 @@ class ProductController extends Controller
'open_source' => 'boolean',
'eu_hosted' => 'boolean',
'eu_made' => 'boolean',
'screenshots' => 'nullable|array',
'screenshots.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
'remove_screenshots' => 'nullable|array',
'remove_screenshots.*' => 'integer',
];
}
@@ -231,7 +235,24 @@ class ProductController extends Controller
*/
protected function syncDetails(Request $request, Product $product): void
{
//
// Only rows belonging to this product may be removed
$ids = array_filter((array) $request->input('remove_screenshots', []));
if ($ids) {
$product->screenshots()->whereIn('id', $ids)->get()->each(function ($shot) {
Storage::disk('public')->delete($shot->path);
$shot->delete();
});
}
if ($request->hasFile('screenshots')) {
$position = ($product->screenshots()->max('position') ?? -1) + 1;
foreach ($request->file('screenshots') as $file) {
$product->screenshots()->create([
'path' => $file->store('screenshots', 'public'),
'position' => $position++,
]);
}
}
}
/**
@@ -246,6 +267,10 @@ class ProductController extends Controller
Storage::disk('public')->delete($product->thumbnail);
}
foreach ($product->screenshots as $shot) {
Storage::disk('public')->delete($shot->path);
}
$product->delete();
return redirect()->route('products.index')
+9
View File
@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
class Product extends Model
@@ -121,6 +122,14 @@ class Product extends Model
return $query->exists();
}
/**
* Get the product's screenshots in display order.
*/
public function screenshots(): HasMany
{
return $this->hasMany(ProductScreenshot::class)->orderBy('position')->orderBy('id');
}
/**
* Get the user that owns the product.
*/
+22
View File
@@ -0,0 +1,22 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class ProductScreenshot extends Model
{
protected $fillable = ['product_id', 'path', 'position'];
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
protected function url(): Attribute
{
return Attribute::get(fn () => asset('storage/'.$this->path));
}
}