| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- <?php
-
- namespace App\Models;
-
- use Illuminate\Database\Eloquent\Attributes\Fillable;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Support\Facades\Storage;
-
- /**
- * One photo of a product. The file lives on the "public" storage disk.
- *
- * @property int $id
- * @property int $store_id
- * @property int $product_id
- * @property string $path
- * @property int $position
- */
- #[Fillable(['store_id', 'path', 'position'])]
- class ProductImage extends Model
- {
- protected static function booted(): void
- {
- static::deleted(function (ProductImage $image): void {
- Storage::disk('public')->delete($image->path);
- });
- }
-
- /**
- * @return BelongsTo<Product, $this>
- */
- public function product(): BelongsTo
- {
- return $this->belongsTo(Product::class);
- }
-
- /**
- * Address of the photo, without the site name, so it works on any local port.
- */
- public function url(): string
- {
- return '/storage/'.$this->path;
- }
- }
|