Product.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Shop\Products;
  3. use App\Shop\Brands\Brand;
  4. use App\Shop\Categories\Category;
  5. use App\Shop\ProductAttributes\ProductAttribute;
  6. use App\Shop\ProductImages\ProductImage;
  7. use Gloudemans\Shoppingcart\Contracts\Buyable;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Support\Collection;
  10. use Nicolaslopezj\Searchable\SearchableTrait;
  11. class Product extends Model implements Buyable
  12. {
  13. use SearchableTrait;
  14. public const MASS_UNIT = [
  15. 'OUNCES' => 'oz',
  16. 'GRAMS' => 'gms',
  17. 'POUNDS' => 'lbs'
  18. ];
  19. public const DISTANCE_UNIT = [
  20. 'CENTIMETER' => 'cm',
  21. 'METER' => 'mtr',
  22. 'INCH' => 'in',
  23. 'MILIMETER' => 'mm',
  24. 'FOOT' => 'ft',
  25. 'YARD' => 'yd'
  26. ];
  27. /**
  28. * Searchable rules.
  29. *
  30. * @var array
  31. */
  32. protected $searchable = [
  33. 'columns' => [
  34. 'products.name' => 10,
  35. 'products.description' => 5
  36. ]
  37. ];
  38. /**
  39. * The attributes that are mass assignable.
  40. *
  41. * @var array
  42. */
  43. protected $fillable = [
  44. 'sku',
  45. 'name',
  46. 'description',
  47. 'cover',
  48. 'quantity',
  49. 'price',
  50. 'brand_id',
  51. 'status',
  52. 'weight',
  53. 'mass_unit',
  54. 'status',
  55. 'sale_price',
  56. 'length',
  57. 'width',
  58. 'height',
  59. 'distance_unit',
  60. 'slug',
  61. ];
  62. /**
  63. * The attributes that should be hidden for arrays.
  64. *
  65. * @var array
  66. */
  67. protected $hidden = [];
  68. public function categories()
  69. {
  70. return $this->belongsToMany(Category::class);
  71. }
  72. /**
  73. * Get the identifier of the Buyable item.
  74. *
  75. * @param null $options
  76. * @return int|string
  77. */
  78. public function getBuyableIdentifier($options = null)
  79. {
  80. return $this->id;
  81. }
  82. /**
  83. * Get the description or title of the Buyable item.
  84. *
  85. * @param null $options
  86. * @return string
  87. */
  88. public function getBuyableDescription($options = null)
  89. {
  90. return $this->name;
  91. }
  92. /**
  93. * Get the price of the Buyable item.
  94. *
  95. * @param null $options
  96. * @return float
  97. */
  98. public function getBuyablePrice($options = null)
  99. {
  100. return $this->price;
  101. }
  102. /**
  103. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  104. */
  105. public function images()
  106. {
  107. return $this->hasMany(ProductImage::class);
  108. }
  109. /**
  110. * @param string $term
  111. * @return Collection
  112. */
  113. public function searchProduct(string $term) : Collection
  114. {
  115. return self::search($term)->get();
  116. }
  117. /**
  118. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  119. */
  120. public function attributes()
  121. {
  122. return $this->hasMany(ProductAttribute::class);
  123. }
  124. /**
  125. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  126. */
  127. public function brand()
  128. {
  129. return $this->belongsTo(Brand::class);
  130. }
  131. }