| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
-
- namespace App\Models;
-
- use App\Enums\ProductStatus;
- use Carbon\CarbonInterface;
- use Database\Factories\ProductFactory;
- use Illuminate\Database\Eloquent\Attributes\Fillable;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Support\Str;
-
- /**
- * Something a store sells. Prices are saved in sen (1 ringgit = 100 sen).
- *
- * @property int $id
- * @property int $store_id
- * @property string $name
- * @property string $slug
- * @property string|null $description
- * @property int $price
- * @property int|null $compare_at_price
- * @property int $stock
- * @property ProductStatus $status
- * @property CarbonInterface|null $created_at
- * @property CarbonInterface|null $updated_at
- * @property-read Store $store
- * @property-read Collection<int, ProductImage> $images
- */
- #[Fillable(['name', 'description', 'price', 'compare_at_price', 'stock', 'status'])]
- class Product extends Model
- {
- /** @use HasFactory<ProductFactory> */
- use HasFactory;
-
- protected static function booted(): void
- {
- static::creating(function (Product $product): void {
- $product->slug = $product->uniqueSlug($product->name);
- });
-
- // The database removes the image rows by itself. The files need removing here.
- static::deleting(function (Product $product): void {
- $product->images->each->delete();
- });
- }
-
- /**
- * A web-address name made from the product name, unique within its store.
- */
- private function uniqueSlug(string $name): string
- {
- $base = Str::slug($name) ?: 'product';
- $slug = $base;
- $suffix = 2;
-
- while (static::query()->where('store_id', $this->store_id)->where('slug', $slug)->exists()) {
- $slug = $base.'-'.$suffix++;
- }
-
- return $slug;
- }
-
- /**
- * @return BelongsTo<Store, $this>
- */
- public function store(): BelongsTo
- {
- return $this->belongsTo(Store::class);
- }
-
- /**
- * @return HasMany<ProductImage, $this>
- */
- public function images(): HasMany
- {
- return $this->hasMany(ProductImage::class)->orderBy('position')->orderBy('id');
- }
-
- /**
- * Products shoppers may see.
- *
- * @param Builder<Product> $query
- */
- public function scopeActive(Builder $query): void
- {
- $query->where('status', ProductStatus::Active);
- }
-
- /**
- * What the storefront is allowed to see. Prices are in ringgit here.
- *
- * @return array<string, mixed>
- */
- public function toStorefront(): array
- {
- $images = $this->images->map(fn (ProductImage $image) => $image->url())->all();
-
- return [
- 'id' => $this->id,
- 'name' => $this->name,
- 'slug' => $this->slug,
- 'description' => $this->description,
- 'price' => $this->price / 100,
- 'compare_at_price' => $this->compare_at_price !== null ? $this->compare_at_price / 100 : null,
- 'in_stock' => $this->stock > 0,
- 'image' => $images[0] ?? null,
- 'images' => $images,
- ];
- }
-
- /**
- * @return array<string, string>
- */
- protected function casts(): array
- {
- return [
- 'status' => ProductStatus::class,
- 'price' => 'integer',
- 'compare_at_price' => 'integer',
- 'stock' => 'integer',
- ];
- }
- }
|