|null $photos * @property string|null $video_url * @property string|null $description * @property string|null $size_chart * @property bool $is_featured * @property CarbonImmutable|null $published_at * @property int|null $published_by * @property array|null $specs * @property CarbonImmutable|null $created_at * @property CarbonImmutable|null $updated_at * @property CarbonImmutable|null $deleted_at * @property-read Category|null $category * @property-read User|null $publishedByRef * @property-read Collection $productVariants * @property-read Collection $reviews * @property-read Collection $coupons * @property-read Collection $tags */ #[ObservedBy(ProductObserver::class)] #[Fillable([ 'category_id', 'name', 'slug', 'sku', 'status', 'price', 'compare_at_price', 'cost_price', 'photos', 'video_url', 'description', 'size_chart', 'is_featured', 'published_at', 'published_by', 'specs', // crud:fillable ])] #[UsePolicy(ProductPolicy::class)] class Product extends Model { /** @use HasFactory */ use HasFactory, SoftDeletes; /** * The columns the edit form writes. A form's version covers these, so a * column the application moves on its own is no conflict for an editor. * * @var list */ public const array EDITABLE = [ 'category_id', 'name', 'slug', 'sku', 'status', 'price', 'compare_at_price', 'cost_price', 'photos', 'video_url', 'description', 'size_chart', 'is_featured', 'specs', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'status' => ProductStatus::class, 'price' => 'decimal:2', 'compare_at_price' => 'decimal:2', 'cost_price' => 'decimal:2', 'photos' => 'array', 'description' => RichTextHtml::class, 'is_featured' => 'boolean', 'published_at' => 'datetime', 'specs' => KeyValueMap::class, // crud:casts ]; } /** * Get the category that owns the product. * * @return BelongsTo */ public function category(): BelongsTo { return $this->belongsTo(Category::class)->withTrashed(); } /** * Get the published by ref that owns the product. * * @return BelongsTo */ public function publishedByRef(): BelongsTo { return $this->belongsTo(User::class, 'published_by')->withTrashed(); } /** * Get the product variants for the product. * * @return HasMany */ public function productVariants(): HasMany { return $this->hasMany(ProductVariant::class, 'product_id'); } /** * Get the reviews for the product. * * @return HasMany */ public function reviews(): HasMany { return $this->hasMany(Review::class, 'product_id'); } /** * Get all of the product's coupons. * * @return MorphMany */ public function coupons(): MorphMany { return $this->morphMany(Coupon::class, 'applies_to'); } /** * The tags that belong to the product. * * @return BelongsToMany */ public function tags(): BelongsToMany { return $this->belongsToMany(Tag::class, 'product_tag', 'product_id', 'tag_id'); } /** * Case-insensitive search across name + sku. * * The operator is chosen per connection by `Search` — `ilike` on * Postgres so the pg_trgm GIN indexes answer it, `like` elsewhere. * * @param Builder $query */ public function scopeSearch(Builder $query, string $term): void { Search::across($query, [ 'name' => Search::TEXT, 'sku' => Search::TEXT, ], $term); } /** * The "booted" method of the model. */ protected static function booted(): void { static::saving(function (self $model): void { // A typed slug is normalised; one that normalises to nothing (`!!!`) // falls back to the source, as a blank one does. $base = Str::slug((string) $model->slug) ?: Str::slug((string) $model->name); $slug = $base; $n = 2; while (static::withTrashed()->whereKeyNot($model->getKey() ?? 0)->where('slug', $slug)->exists()) { $slug = $base.'-'.$n++; } $model->slug = $slug; }); } /** * The sizes each `image;variants` column keeps its uploads in — the * longest edge, in pixels — built by MakeImageVariants after the write. * * @var array> */ public const array IMAGE_VARIANTS = [ 'photos' => ['thumb' => 320, 'medium' => 800, 'large' => 1600], ]; // crud:custom — relations, scopes and helpers written by hand go between // these two markers; `make:crud --force` carries them into the new model. /** * The rows the public API may show (`make:crud --public`) — every row * until you narrow it. Written once by the generator and yours from * then on: say what anyone may browse, for instance * * $query->where('status', 'active')->where('published_at', '<=', now()); * * A row this leaves out is not listed and its URL answers 404; rows of * other public modules embedded with `?include` go through their own. * * @param Builder $query */ public function scopePubliclyVisible(Builder $query): void { // A shop shows what it sells: drafts and archived lines stay out. $query->where('status', ProductStatus::Active); } // crud:custom:end }