|null $plan * @property Carbon|null $created_at * @property Carbon|null $updated_at */ #[Fillable(['name', 'category', 'description', 'language'])] class Store extends Model { /** @use HasFactory */ use HasFactory; /** * Slugs that can never be used, because they will clash with our own subdomains. * * @var list */ public const array RESERVED_SLUGS = ['www', 'app', 'api', 'admin', 'dashboard', 'mail', 'store', 'stores', 'create']; /** * Default values for new stores. * * @var array */ protected $attributes = [ 'language' => 'en', 'status' => 'draft', ]; protected static function booted(): void { static::creating(function (Store $store) { if (blank($store->slug)) { $store->slug = static::uniqueSlug($store->name); } }); } /** * Build a web-safe slug from a store name that no other store is using. */ public static function uniqueSlug(string $name): string { $base = Str::slug($name) ?: 'store'; $slug = $base; $suffix = 2; while (in_array($slug, self::RESERVED_SLUGS, true) || static::query()->where('slug', $slug)->exists()) { $slug = $base.'-'.$suffix++; } return $slug; } public function getRouteKeyName(): string { return 'slug'; } /** * @return BelongsTo */ public function user(): BelongsTo { return $this->belongsTo(User::class); } /** * The AI jobs run for this store, newest first. * * @return HasMany */ public function generations(): HasMany { return $this->hasMany(StoreGeneration::class)->latest('id'); } /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'category' => StoreCategory::class, 'language' => StoreLanguage::class, 'status' => StoreStatus::class, 'plan' => 'array', ]; } }