*/ public function product(): BelongsTo { return $this->belongsTo(Product::class); } /** * The option values in order, without empty ones. * * @return list */ public function values(): array { return array_values(array_filter([$this->option1, $this->option2], fn (?string $value) => $value !== null && $value !== '')); } /** * "500g / Pedas". */ public function name(): string { return implode(' / ', $this->values()); } /** * What the storefront is allowed to see. Prices are in ringgit here. * * @return array */ public function toStorefront(): array { return [ 'id' => $this->id, 'name' => $this->name(), 'options' => $this->values(), 'price' => $this->price / 100, 'compare_at_price' => $this->compare_at_price !== null ? $this->compare_at_price / 100 : null, 'in_stock' => $this->stock > 0, 'stock_left' => $this->stock, ]; } /** * @return array */ protected function casts(): array { return [ 'price' => 'integer', 'compare_at_price' => 'integer', 'stock' => 'integer', 'position' => 'integer', ]; } }