AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\ProductStatus;
  4. use Carbon\CarbonInterface;
  5. use Database\Factories\ProductFactory;
  6. use Illuminate\Database\Eloquent\Attributes\Fillable;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Database\Eloquent\Collection;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  12. use Illuminate\Database\Eloquent\Relations\HasMany;
  13. use Illuminate\Support\Str;
  14. /**
  15. * Something a store sells. Prices are saved in sen (1 ringgit = 100 sen).
  16. *
  17. * @property int $id
  18. * @property int $store_id
  19. * @property string $name
  20. * @property string $slug
  21. * @property string|null $description
  22. * @property list<array{name: string, values: list<string>}>|null $options Up to 2 options. Null for a simple product.
  23. * @property int $price
  24. * @property int|null $compare_at_price
  25. * @property int $stock
  26. * @property ProductStatus $status
  27. * @property CarbonInterface|null $created_at
  28. * @property CarbonInterface|null $updated_at
  29. * @property-read Store $store
  30. * @property-read Collection<int, ProductImage> $images
  31. * @property-read Collection<int, ProductVariant> $variants
  32. */
  33. #[Fillable(['name', 'description', 'price', 'compare_at_price', 'stock', 'status'])]
  34. class Product extends Model
  35. {
  36. /** @use HasFactory<ProductFactory> */
  37. use HasFactory;
  38. protected static function booted(): void
  39. {
  40. static::creating(function (Product $product): void {
  41. $product->slug = $product->uniqueSlug($product->name);
  42. });
  43. // The database removes the image rows by itself. The files need removing here.
  44. static::deleting(function (Product $product): void {
  45. $product->images->each->delete();
  46. });
  47. }
  48. /**
  49. * A web-address name made from the product name, unique within its store.
  50. */
  51. private function uniqueSlug(string $name): string
  52. {
  53. $base = Str::slug($name) ?: 'product';
  54. $slug = $base;
  55. $suffix = 2;
  56. while (static::query()->where('store_id', $this->store_id)->where('slug', $slug)->exists()) {
  57. $slug = $base.'-'.$suffix++;
  58. }
  59. return $slug;
  60. }
  61. /**
  62. * @return BelongsTo<Store, $this>
  63. */
  64. public function store(): BelongsTo
  65. {
  66. return $this->belongsTo(Store::class);
  67. }
  68. /**
  69. * @return HasMany<ProductImage, $this>
  70. */
  71. public function images(): HasMany
  72. {
  73. return $this->hasMany(ProductImage::class)->orderBy('position')->orderBy('id');
  74. }
  75. /**
  76. * @return HasMany<ProductVariant, $this>
  77. */
  78. public function variants(): HasMany
  79. {
  80. return $this->hasMany(ProductVariant::class)->orderBy('position')->orderBy('id');
  81. }
  82. /**
  83. * True when the product comes in options, such as sizes. Then price and stock live on each variant.
  84. */
  85. public function hasVariants(): bool
  86. {
  87. return ! empty($this->options);
  88. }
  89. /**
  90. * For a product with options: the product's own price and stock follow its variants
  91. * (the lowest price, and the stock of all variants together), so lists and cards stay right.
  92. */
  93. public function syncFromVariants(): void
  94. {
  95. $variants = $this->variants()->get();
  96. if (! $this->hasVariants() || $variants->isEmpty()) {
  97. return;
  98. }
  99. $cheapest = $variants->sortBy('price')->first();
  100. $this->price = (int) $cheapest->price;
  101. $this->compare_at_price = $cheapest->compare_at_price;
  102. $this->stock = (int) $variants->sum('stock');
  103. $this->save();
  104. }
  105. /**
  106. * Products shoppers may see.
  107. *
  108. * @param Builder<Product> $query
  109. */
  110. public function scopeActive(Builder $query): void
  111. {
  112. $query->where('status', ProductStatus::Active);
  113. }
  114. /**
  115. * What the storefront is allowed to see. Prices are in ringgit here.
  116. *
  117. * @return array<string, mixed>
  118. */
  119. public function toStorefront(): array
  120. {
  121. $images = $this->images->map(fn (ProductImage $image) => $image->url())->all();
  122. return [
  123. 'id' => $this->id,
  124. 'name' => $this->name,
  125. 'slug' => $this->slug,
  126. 'description' => $this->description,
  127. 'price' => $this->price / 100,
  128. 'compare_at_price' => $this->compare_at_price !== null ? $this->compare_at_price / 100 : null,
  129. 'in_stock' => $this->stock > 0,
  130. 'stock_left' => $this->stock,
  131. 'image' => $images[0] ?? null,
  132. 'images' => $images,
  133. 'options' => $this->hasVariants() ? $this->options : [],
  134. 'variants' => $this->hasVariants()
  135. ? $this->variants->map(fn (ProductVariant $variant) => $variant->toStorefront())->all()
  136. : [],
  137. // True when variants cost different amounts, so lists show "From RM X".
  138. 'price_varies' => $this->hasVariants() && $this->variants->pluck('price')->unique()->count() > 1,
  139. ];
  140. }
  141. /**
  142. * @return array<string, string>
  143. */
  144. protected function casts(): array
  145. {
  146. return [
  147. 'status' => ProductStatus::class,
  148. 'price' => 'integer',
  149. 'compare_at_price' => 'integer',
  150. 'stock' => 'integer',
  151. 'options' => 'array',
  152. ];
  153. }
  154. }