| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
-
- namespace App\Models;
-
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
-
- /**
- * One thing inside a bundle: a product (and, for a product with options, one of its variants) and how many.
- *
- * @property int $id
- * @property int $store_id
- * @property int $bundle_id
- * @property int $product_id
- * @property int|null $variant_id
- * @property int $quantity
- * @property int $position
- * @property-read Product $bundle
- * @property-read Product $product
- * @property-read ProductVariant|null $variant
- */
- class BundleItem extends Model
- {
- /**
- * @return BelongsTo<Product, $this>
- */
- public function bundle(): BelongsTo
- {
- return $this->belongsTo(Product::class, 'bundle_id');
- }
-
- /**
- * @return BelongsTo<Product, $this>
- */
- public function product(): BelongsTo
- {
- return $this->belongsTo(Product::class);
- }
-
- /**
- * @return BelongsTo<ProductVariant, $this>
- */
- public function variant(): BelongsTo
- {
- return $this->belongsTo(ProductVariant::class, 'variant_id');
- }
-
- /**
- * "Sambal Hijau (250g)".
- */
- public function name(): string
- {
- return $this->product->name.($this->variant !== null ? ' ('.$this->variant->name().')' : '');
- }
-
- /**
- * "2 × Sambal Hijau (250g)".
- */
- public function label(): string
- {
- return $this->quantity.' × '.$this->name();
- }
-
- /**
- * How many of this item are left: the variant's stock, or the product's.
- */
- public function stock(): int
- {
- return $this->variant !== null ? $this->variant->stock : $this->product->stock;
- }
-
- /**
- * @return array<string, string>
- */
- protected function casts(): array
- {
- return [
- 'quantity' => 'integer',
- 'position' => 'integer',
- ];
- }
- }
|