Payment.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. /**
  10. * 支付单.
  11. */
  12. class Payment extends Model
  13. {
  14. protected $table = 'payment';
  15. protected $guarded = [];
  16. protected $casts = ['amount' => money::class];
  17. public function scopeUid(Builder $query): Builder
  18. {
  19. return $query->whereUserId(Auth::id());
  20. }
  21. public function user(): BelongsTo
  22. {
  23. return $this->belongsTo(User::class);
  24. }
  25. public function failed(): bool
  26. { // 关闭支付单
  27. return $this->close() && $this->order()->close();
  28. }
  29. public function close(): bool
  30. { // 关闭支付单
  31. return $this->update(['status' => -1]);
  32. }
  33. public function order(): BelongsTo
  34. {
  35. return $this->belongsTo(Order::class);
  36. }
  37. public function complete(): bool
  38. { // 完成支付单
  39. return $this->update(['status' => 1]);
  40. }
  41. public function getAmountTagAttribute(): string
  42. {
  43. return Helpers::getPriceTag($this->amount);
  44. }
  45. // 订单状态
  46. public function getStatusLabelAttribute(): string
  47. {
  48. return match ($this->attributes['status']) {
  49. -1 => trans('common.failed_item', ['attribute' => trans('user.pay')]),
  50. 1 => trans('common.success_item', ['attribute' => trans('user.pay')]),
  51. default => trans('common.payment.status.wait'),
  52. };
  53. }
  54. }