|null $plan * @property array|null $published_plan * @property CarbonInterface|null $published_at * @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', 'localhost', 'static', 'assets', 'cdn', 'help', 'support', 'status', 'blog', 'billing', 'login', 'register', ]; /** * 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); } /** * Stores that shoppers can open. * * @param Builder $query */ public function scopePublished(Builder $query): void { $query->where('status', StoreStatus::Published)->whereNotNull('published_plan'); } public function isPublished(): bool { return $this->status === StoreStatus::Published && $this->published_plan !== null; } /** * True when the working design differs from the copy shoppers see. */ public function hasUnpublishedChanges(): bool { return $this->isPublished() && $this->plan != $this->published_plan; } /** * @return HasMany */ public function products(): HasMany { return $this->hasMany(Product::class); } /** * @return HasMany */ public function orders(): HasMany { return $this->hasMany(Order::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', 'published_plan' => 'array', 'published_at' => 'datetime', ]; } }