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.

Store.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\StoreCategory;
  4. use App\Enums\StoreLanguage;
  5. use App\Enums\StoreStatus;
  6. use Carbon\CarbonInterface;
  7. use Database\Factories\StoreFactory;
  8. use Illuminate\Database\Eloquent\Attributes\Fillable;
  9. use Illuminate\Database\Eloquent\Builder;
  10. use Illuminate\Database\Eloquent\Factories\HasFactory;
  11. use Illuminate\Database\Eloquent\Model;
  12. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  13. use Illuminate\Database\Eloquent\Relations\HasMany;
  14. use Illuminate\Support\Carbon;
  15. use Illuminate\Support\Str;
  16. /**
  17. * A merchant's online store. Every store-related table points back here with `store_id`.
  18. *
  19. * @property int $id
  20. * @property int $user_id
  21. * @property string $name
  22. * @property string $slug
  23. * @property StoreCategory $category
  24. * @property string $description
  25. * @property StoreLanguage $language
  26. * @property StoreStatus $status
  27. * @property array<string, mixed>|null $plan
  28. * @property string|null $whatsapp_number
  29. * @property int $shipping_west
  30. * @property int $shipping_east
  31. * @property int|null $free_shipping_over
  32. * @property array<string, mixed>|null $published_plan
  33. * @property CarbonInterface|null $published_at
  34. * @property Carbon|null $created_at
  35. * @property Carbon|null $updated_at
  36. */
  37. #[Fillable(['name', 'category', 'description', 'language'])]
  38. class Store extends Model
  39. {
  40. /** @use HasFactory<StoreFactory> */
  41. use HasFactory;
  42. /**
  43. * Slugs that can never be used, because they will clash with our own subdomains.
  44. *
  45. * @var list<string>
  46. */
  47. public const array RESERVED_SLUGS = [
  48. 'www', 'app', 'api', 'admin', 'dashboard', 'mail', 'store', 'stores', 'create',
  49. 'localhost', 'static', 'assets', 'cdn', 'help', 'support', 'status', 'blog', 'billing', 'login', 'register',
  50. ];
  51. /**
  52. * Default values for new stores.
  53. *
  54. * @var array<string, string|int>
  55. */
  56. protected $attributes = [
  57. 'language' => 'en',
  58. 'status' => 'draft',
  59. // Starting shipping fees in sen, so a new store has sensible numbers before the merchant changes them.
  60. 'shipping_west' => 800,
  61. 'shipping_east' => 1500,
  62. ];
  63. protected static function booted(): void
  64. {
  65. static::creating(function (Store $store) {
  66. if (blank($store->slug)) {
  67. $store->slug = static::uniqueSlug($store->name);
  68. }
  69. });
  70. }
  71. /**
  72. * Build a web-safe slug from a store name that no other store is using.
  73. */
  74. public static function uniqueSlug(string $name): string
  75. {
  76. $base = Str::slug($name) ?: 'store';
  77. $slug = $base;
  78. $suffix = 2;
  79. while (in_array($slug, self::RESERVED_SLUGS, true) || static::query()->where('slug', $slug)->exists()) {
  80. $slug = $base.'-'.$suffix++;
  81. }
  82. return $slug;
  83. }
  84. public function getRouteKeyName(): string
  85. {
  86. return 'slug';
  87. }
  88. /**
  89. * @return BelongsTo<User, $this>
  90. */
  91. public function user(): BelongsTo
  92. {
  93. return $this->belongsTo(User::class);
  94. }
  95. /**
  96. * Stores that shoppers can open.
  97. *
  98. * @param Builder<Store> $query
  99. */
  100. public function scopePublished(Builder $query): void
  101. {
  102. $query->where('status', StoreStatus::Published)->whereNotNull('published_plan');
  103. }
  104. public function isPublished(): bool
  105. {
  106. return $this->status === StoreStatus::Published && $this->published_plan !== null;
  107. }
  108. /**
  109. * True when the working design differs from the copy shoppers see.
  110. */
  111. public function hasUnpublishedChanges(): bool
  112. {
  113. return $this->isPublished() && $this->plan != $this->published_plan;
  114. }
  115. /**
  116. * @return HasMany<Product, $this>
  117. */
  118. public function products(): HasMany
  119. {
  120. return $this->hasMany(Product::class);
  121. }
  122. /**
  123. * Photos uploaded for the design, such as a hero banner.
  124. *
  125. * @return HasMany<StoreImage, $this>
  126. */
  127. public function images(): HasMany
  128. {
  129. return $this->hasMany(StoreImage::class);
  130. }
  131. /**
  132. * @return HasMany<DiscountCode, $this>
  133. */
  134. public function discountCodes(): HasMany
  135. {
  136. return $this->hasMany(DiscountCode::class);
  137. }
  138. /**
  139. * @return HasMany<AutomaticDiscount, $this>
  140. */
  141. public function automaticDiscounts(): HasMany
  142. {
  143. return $this->hasMany(AutomaticDiscount::class);
  144. }
  145. /**
  146. * @return HasMany<Order, $this>
  147. */
  148. public function orders(): HasMany
  149. {
  150. return $this->hasMany(Order::class);
  151. }
  152. /**
  153. * The AI jobs run for this store, newest first.
  154. *
  155. * @return HasMany<StoreGeneration, $this>
  156. */
  157. public function generations(): HasMany
  158. {
  159. return $this->hasMany(StoreGeneration::class)->latest('id');
  160. }
  161. /**
  162. * Get the attributes that should be cast.
  163. *
  164. * @return array<string, string>
  165. */
  166. protected function casts(): array
  167. {
  168. return [
  169. 'category' => StoreCategory::class,
  170. 'language' => StoreLanguage::class,
  171. 'status' => StoreStatus::class,
  172. 'plan' => 'array',
  173. 'published_plan' => 'array',
  174. 'published_at' => 'datetime',
  175. 'shipping_west' => 'integer',
  176. 'shipping_east' => 'integer',
  177. 'free_shipping_over' => 'integer',
  178. ];
  179. }
  180. }