AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

BundleItem.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * One thing inside a bundle: a product (and, for a product with options, one of its variants) and how many.
  7. *
  8. * @property int $id
  9. * @property int $store_id
  10. * @property int $bundle_id
  11. * @property int $product_id
  12. * @property int|null $variant_id
  13. * @property int $quantity
  14. * @property int $position
  15. * @property-read Product $bundle
  16. * @property-read Product $product
  17. * @property-read ProductVariant|null $variant
  18. */
  19. class BundleItem extends Model
  20. {
  21. /**
  22. * @return BelongsTo<Product, $this>
  23. */
  24. public function bundle(): BelongsTo
  25. {
  26. return $this->belongsTo(Product::class, 'bundle_id');
  27. }
  28. /**
  29. * @return BelongsTo<Product, $this>
  30. */
  31. public function product(): BelongsTo
  32. {
  33. return $this->belongsTo(Product::class);
  34. }
  35. /**
  36. * @return BelongsTo<ProductVariant, $this>
  37. */
  38. public function variant(): BelongsTo
  39. {
  40. return $this->belongsTo(ProductVariant::class, 'variant_id');
  41. }
  42. /**
  43. * "Sambal Hijau (250g)".
  44. */
  45. public function name(): string
  46. {
  47. return $this->product->name.($this->variant !== null ? ' ('.$this->variant->name().')' : '');
  48. }
  49. /**
  50. * "2 × Sambal Hijau (250g)".
  51. */
  52. public function label(): string
  53. {
  54. return $this->quantity.' × '.$this->name();
  55. }
  56. /**
  57. * How many of this item are left: the variant's stock, or the product's.
  58. */
  59. public function stock(): int
  60. {
  61. return $this->variant !== null ? $this->variant->stock : $this->product->stock;
  62. }
  63. /**
  64. * @return array<string, string>
  65. */
  66. protected function casts(): array
  67. {
  68. return [
  69. 'quantity' => 'integer',
  70. 'position' => 'integer',
  71. ];
  72. }
  73. }