AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Product.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 int $price
  23. * @property int|null $compare_at_price
  24. * @property int $stock
  25. * @property ProductStatus $status
  26. * @property CarbonInterface|null $created_at
  27. * @property CarbonInterface|null $updated_at
  28. * @property-read Store $store
  29. * @property-read Collection<int, ProductImage> $images
  30. */
  31. #[Fillable(['name', 'description', 'price', 'compare_at_price', 'stock', 'status'])]
  32. class Product extends Model
  33. {
  34. /** @use HasFactory<ProductFactory> */
  35. use HasFactory;
  36. protected static function booted(): void
  37. {
  38. static::creating(function (Product $product): void {
  39. $product->slug = $product->uniqueSlug($product->name);
  40. });
  41. // The database removes the image rows by itself. The files need removing here.
  42. static::deleting(function (Product $product): void {
  43. $product->images->each->delete();
  44. });
  45. }
  46. /**
  47. * A web-address name made from the product name, unique within its store.
  48. */
  49. private function uniqueSlug(string $name): string
  50. {
  51. $base = Str::slug($name) ?: 'product';
  52. $slug = $base;
  53. $suffix = 2;
  54. while (static::query()->where('store_id', $this->store_id)->where('slug', $slug)->exists()) {
  55. $slug = $base.'-'.$suffix++;
  56. }
  57. return $slug;
  58. }
  59. /**
  60. * @return BelongsTo<Store, $this>
  61. */
  62. public function store(): BelongsTo
  63. {
  64. return $this->belongsTo(Store::class);
  65. }
  66. /**
  67. * @return HasMany<ProductImage, $this>
  68. */
  69. public function images(): HasMany
  70. {
  71. return $this->hasMany(ProductImage::class)->orderBy('position')->orderBy('id');
  72. }
  73. /**
  74. * Products shoppers may see.
  75. *
  76. * @param Builder<Product> $query
  77. */
  78. public function scopeActive(Builder $query): void
  79. {
  80. $query->where('status', ProductStatus::Active);
  81. }
  82. /**
  83. * What the storefront is allowed to see. Prices are in ringgit here.
  84. *
  85. * @return array<string, mixed>
  86. */
  87. public function toStorefront(): array
  88. {
  89. $images = $this->images->map(fn (ProductImage $image) => $image->url())->all();
  90. return [
  91. 'id' => $this->id,
  92. 'name' => $this->name,
  93. 'slug' => $this->slug,
  94. 'description' => $this->description,
  95. 'price' => $this->price / 100,
  96. 'compare_at_price' => $this->compare_at_price !== null ? $this->compare_at_price / 100 : null,
  97. 'in_stock' => $this->stock > 0,
  98. 'image' => $images[0] ?? null,
  99. 'images' => $images,
  100. ];
  101. }
  102. /**
  103. * @return array<string, string>
  104. */
  105. protected function casts(): array
  106. {
  107. return [
  108. 'status' => ProductStatus::class,
  109. 'price' => 'integer',
  110. 'compare_at_price' => 'integer',
  111. 'stock' => 'integer',
  112. ];
  113. }
  114. }