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 2.7KB

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