ReferralLog.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Models;
  3. use App\Casts\money;
  4. use App\Utils\Helpers;
  5. use Auth;
  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\BelongsTo;
  10. /**
  11. * 返利日志.
  12. */
  13. class ReferralLog extends Model
  14. {
  15. protected $table = 'referral_log';
  16. protected $casts = ['amount' => money::class, 'commission' => money::class];
  17. protected $guarded = [];
  18. public function scopeUid(Builder $query): Builder
  19. {
  20. return $query->whereInviterId(Auth::id());
  21. }
  22. public function invitee(): BelongsTo
  23. {
  24. return $this->belongsTo(User::class);
  25. }
  26. public function inviter(): BelongsTo
  27. {
  28. return $this->belongsTo(User::class);
  29. }
  30. public function order(): BelongsTo
  31. {
  32. return $this->belongsTo(Order::class);
  33. }
  34. protected function amountTag(): Attribute
  35. {
  36. return Attribute::make(
  37. get: fn () => Helpers::getPriceTag($this->amount),
  38. );
  39. }
  40. protected function commissionTag(): Attribute
  41. {
  42. return Attribute::make(
  43. get: fn () => Helpers::getPriceTag($this->commission),
  44. );
  45. }
  46. protected function statusLabel(): Attribute
  47. {
  48. return Attribute::make(
  49. get: fn () => match ($this->status) {
  50. 1 => '<span class="badge badge-sm badge-info">'.trans('common.status.applying').'</span>',
  51. 2 => '<span class="badge badge-sm badge-default">'.trans('common.status.withdrawn').'</span>',
  52. default => '<span class="badge badge-sm badge-success">'.trans('common.status.withdrawal_pending').'</span>',
  53. },
  54. );
  55. }
  56. }