AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Store.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\StoreCategory;
  4. use App\Enums\StoreLanguage;
  5. use App\Enums\StoreStatus;
  6. use Database\Factories\StoreFactory;
  7. use Illuminate\Database\Eloquent\Attributes\Fillable;
  8. use Illuminate\Database\Eloquent\Factories\HasFactory;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  11. use Illuminate\Database\Eloquent\Relations\HasMany;
  12. use Illuminate\Support\Carbon;
  13. use Illuminate\Support\Str;
  14. /**
  15. * A merchant's online store. Every store-related table points back here with `store_id`.
  16. *
  17. * @property int $id
  18. * @property int $user_id
  19. * @property string $name
  20. * @property string $slug
  21. * @property StoreCategory $category
  22. * @property string $description
  23. * @property StoreLanguage $language
  24. * @property StoreStatus $status
  25. * @property array<string, mixed>|null $plan
  26. * @property Carbon|null $created_at
  27. * @property Carbon|null $updated_at
  28. */
  29. #[Fillable(['name', 'category', 'description', 'language'])]
  30. class Store extends Model
  31. {
  32. /** @use HasFactory<StoreFactory> */
  33. use HasFactory;
  34. /**
  35. * Slugs that can never be used, because they will clash with our own subdomains.
  36. *
  37. * @var list<string>
  38. */
  39. public const array RESERVED_SLUGS = ['www', 'app', 'api', 'admin', 'dashboard', 'mail', 'store', 'stores', 'create'];
  40. /**
  41. * Default values for new stores.
  42. *
  43. * @var array<string, string>
  44. */
  45. protected $attributes = [
  46. 'language' => 'en',
  47. 'status' => 'draft',
  48. ];
  49. protected static function booted(): void
  50. {
  51. static::creating(function (Store $store) {
  52. if (blank($store->slug)) {
  53. $store->slug = static::uniqueSlug($store->name);
  54. }
  55. });
  56. }
  57. /**
  58. * Build a web-safe slug from a store name that no other store is using.
  59. */
  60. public static function uniqueSlug(string $name): string
  61. {
  62. $base = Str::slug($name) ?: 'store';
  63. $slug = $base;
  64. $suffix = 2;
  65. while (in_array($slug, self::RESERVED_SLUGS, true) || static::query()->where('slug', $slug)->exists()) {
  66. $slug = $base.'-'.$suffix++;
  67. }
  68. return $slug;
  69. }
  70. public function getRouteKeyName(): string
  71. {
  72. return 'slug';
  73. }
  74. /**
  75. * @return BelongsTo<User, $this>
  76. */
  77. public function user(): BelongsTo
  78. {
  79. return $this->belongsTo(User::class);
  80. }
  81. /**
  82. * @return HasMany<Product, $this>
  83. */
  84. public function products(): HasMany
  85. {
  86. return $this->hasMany(Product::class);
  87. }
  88. /**
  89. * The AI jobs run for this store, newest first.
  90. *
  91. * @return HasMany<StoreGeneration, $this>
  92. */
  93. public function generations(): HasMany
  94. {
  95. return $this->hasMany(StoreGeneration::class)->latest('id');
  96. }
  97. /**
  98. * Get the attributes that should be cast.
  99. *
  100. * @return array<string, string>
  101. */
  102. protected function casts(): array
  103. {
  104. return [
  105. 'category' => StoreCategory::class,
  106. 'language' => StoreLanguage::class,
  107. 'status' => StoreStatus::class,
  108. 'plan' => 'array',
  109. ];
  110. }
  111. }