Payment.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Models;
  3. use Auth;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. /**
  7. * 支付单.
  8. */
  9. class Payment extends Model
  10. {
  11. protected $table = 'payment';
  12. protected $guarded = [];
  13. public function scopeUid($query)
  14. {
  15. return $query->whereUserId(Auth::id());
  16. }
  17. public function user(): BelongsTo
  18. {
  19. return $this->belongsTo(User::class);
  20. }
  21. public function order(): BelongsTo
  22. {
  23. return $this->belongsTo(Order::class);
  24. }
  25. public function close() // 关闭支付单
  26. {
  27. return $this->update(['status' => -1]);
  28. }
  29. public function complete() // 完成支付单
  30. {
  31. return $this->update(['status' => 1]);
  32. }
  33. public function getAmountAttribute($value)
  34. {
  35. return $value / 100;
  36. }
  37. public function setAmountAttribute($value)
  38. {
  39. return $this->attributes['amount'] = $value * 100;
  40. }
  41. // 订单状态
  42. public function getStatusLabelAttribute(): string
  43. {
  44. switch ($this->attributes['status']) {
  45. case -1:
  46. $status_label = '支付失败';
  47. break;
  48. case 1:
  49. $status_label = '支付成功';
  50. break;
  51. case 0:
  52. default:
  53. $status_label = '等待支付';
  54. }
  55. return $status_label;
  56. }
  57. }