Goods.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 category(): BelongsTo
  25. {
  26. return $this->belongsTo(GoodsCategory::class, 'category_id');
  27. }
  28. public function scopeType(Builder $query, int $type): Builder
  29. {
  30. return $query->whereType($type)->whereStatus(1)->orderByDesc('sort');
  31. }
  32. protected function priceTag(): Attribute
  33. {
  34. return Attribute::make(
  35. get: fn () => Helpers::getPriceTag($this->price),
  36. );
  37. }
  38. protected function renewTag(): Attribute
  39. {
  40. return Attribute::make(
  41. get: fn () => Helpers::getPriceTag($this->renew),
  42. );
  43. }
  44. protected function trafficLabel(): Attribute
  45. {
  46. return Attribute::make(
  47. get: fn () => formatBytes($this->traffic, 'MiB'),
  48. );
  49. }
  50. }