AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Store.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 array<string, mixed>|null $published_plan
  29. * @property CarbonInterface|null $published_at
  30. * @property Carbon|null $created_at
  31. * @property Carbon|null $updated_at
  32. */
  33. #[Fillable(['name', 'category', 'description', 'language'])]
  34. class Store extends Model
  35. {
  36. /** @use HasFactory<StoreFactory> */
  37. use HasFactory;
  38. /**
  39. * Slugs that can never be used, because they will clash with our own subdomains.
  40. *
  41. * @var list<string>
  42. */
  43. public const array RESERVED_SLUGS = [
  44. 'www', 'app', 'api', 'admin', 'dashboard', 'mail', 'store', 'stores', 'create',
  45. 'localhost', 'static', 'assets', 'cdn', 'help', 'support', 'status', 'blog', 'billing', 'login', 'register',
  46. ];
  47. /**
  48. * Default values for new stores.
  49. *
  50. * @var array<string, string>
  51. */
  52. protected $attributes = [
  53. 'language' => 'en',
  54. 'status' => 'draft',
  55. ];
  56. protected static function booted(): void
  57. {
  58. static::creating(function (Store $store) {
  59. if (blank($store->slug)) {
  60. $store->slug = static::uniqueSlug($store->name);
  61. }
  62. });
  63. }
  64. /**
  65. * Build a web-safe slug from a store name that no other store is using.
  66. */
  67. public static function uniqueSlug(string $name): string
  68. {
  69. $base = Str::slug($name) ?: 'store';
  70. $slug = $base;
  71. $suffix = 2;
  72. while (in_array($slug, self::RESERVED_SLUGS, true) || static::query()->where('slug', $slug)->exists()) {
  73. $slug = $base.'-'.$suffix++;
  74. }
  75. return $slug;
  76. }
  77. public function getRouteKeyName(): string
  78. {
  79. return 'slug';
  80. }
  81. /**
  82. * @return BelongsTo<User, $this>
  83. */
  84. public function user(): BelongsTo
  85. {
  86. return $this->belongsTo(User::class);
  87. }
  88. /**
  89. * Stores that shoppers can open.
  90. *
  91. * @param Builder<Store> $query
  92. */
  93. public function scopePublished(Builder $query): void
  94. {
  95. $query->where('status', StoreStatus::Published)->whereNotNull('published_plan');
  96. }
  97. public function isPublished(): bool
  98. {
  99. return $this->status === StoreStatus::Published && $this->published_plan !== null;
  100. }
  101. /**
  102. * True when the working design differs from the copy shoppers see.
  103. */
  104. public function hasUnpublishedChanges(): bool
  105. {
  106. return $this->isPublished() && $this->plan != $this->published_plan;
  107. }
  108. /**
  109. * @return HasMany<Product, $this>
  110. */
  111. public function products(): HasMany
  112. {
  113. return $this->hasMany(Product::class);
  114. }
  115. /**
  116. * @return HasMany<Order, $this>
  117. */
  118. public function orders(): HasMany
  119. {
  120. return $this->hasMany(Order::class);
  121. }
  122. /**
  123. * The AI jobs run for this store, newest first.
  124. *
  125. * @return HasMany<StoreGeneration, $this>
  126. */
  127. public function generations(): HasMany
  128. {
  129. return $this->hasMany(StoreGeneration::class)->latest('id');
  130. }
  131. /**
  132. * Get the attributes that should be cast.
  133. *
  134. * @return array<string, string>
  135. */
  136. protected function casts(): array
  137. {
  138. return [
  139. 'category' => StoreCategory::class,
  140. 'language' => StoreLanguage::class,
  141. 'status' => StoreStatus::class,
  142. 'plan' => 'array',
  143. 'published_plan' => 'array',
  144. 'published_at' => 'datetime',
  145. ];
  146. }
  147. }