Goods.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Models;
  3. use App\Casts\data_rate;
  4. use App\Casts\money;
  5. use App\Utils\Helpers;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Database\Eloquent\Casts\Attribute;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\Relations\HasMany;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. /**
  12. * 商品
  13. */
  14. class Goods extends Model
  15. {
  16. use SoftDeletes;
  17. protected $table = 'goods';
  18. protected $casts = ['price' => money::class, 'renew' => money::class, 'speed_limit' => data_rate::class, 'deleted_at' => 'datetime'];
  19. protected $guarded = [];
  20. public function orders(): HasMany
  21. {
  22. return $this->hasMany(Order::class);
  23. }
  24. public function scopeType(Builder $query, int $type): Builder
  25. {
  26. return $query->whereType($type)->whereStatus(1)->orderByDesc('sort');
  27. }
  28. protected function priceTag(): Attribute
  29. {
  30. return Attribute::make(
  31. get: fn () => Helpers::getPriceTag($this->price),
  32. );
  33. }
  34. protected function renewTag(): Attribute
  35. {
  36. return Attribute::make(
  37. get: fn () => Helpers::getPriceTag($this->renew),
  38. );
  39. }
  40. protected function trafficLabel(): Attribute
  41. {
  42. return Attribute::make(
  43. get: fn () => formatBytes($this->traffic, 'MiB'),
  44. );
  45. }
  46. }