Goods.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Models;
  3. use App\Components\Helpers;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. /**
  8. * 商品
  9. */
  10. class Goods extends Model
  11. {
  12. use SoftDeletes;
  13. protected $table = 'goods';
  14. protected $dates = ['deleted_at'];
  15. protected $guarded = [];
  16. public function orders(): HasMany
  17. {
  18. return $this->hasMany(Order::class);
  19. }
  20. public function scopeType($query, $type)
  21. {
  22. return $query->whereType($type)->whereStatus(1)->orderByDesc('sort');
  23. }
  24. public function getPriceAttribute($value)
  25. {
  26. return $value / 100;
  27. }
  28. public function setPriceAttribute($value)
  29. {
  30. $this->attributes['price'] = $value * 100;
  31. }
  32. public function getPriceTagAttribute(): string
  33. {
  34. return Helpers::getPriceTag($this->price);
  35. }
  36. public function getRenewAttribute($value)
  37. {
  38. return $value / 100;
  39. }
  40. public function setRenewAttribute($value)
  41. {
  42. $this->attributes['renew'] = $value * 100;
  43. }
  44. public function getRenewTagAttribute(): string
  45. {
  46. return Helpers::getPriceTag($this->renew);
  47. }
  48. public function getTrafficLabelAttribute()
  49. {
  50. return flowAutoShow($this->attributes['traffic'] * MB);
  51. }
  52. public function setSpeedLimitAttribute($value)
  53. {
  54. return $this->attributes['speed_limit'] = $value * Mbps;
  55. }
  56. public function getSpeedLimitAttribute($value)
  57. {
  58. return $value / Mbps;
  59. }
  60. }