Payment.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Http\Models;
  3. use Auth;
  4. use Eloquent;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Carbon;
  8. /**
  9. * 支付单
  10. * Class Payment
  11. *
  12. * @property int $id
  13. * @property string|null $sn
  14. * @property int $user_id 用户ID
  15. * @property int|null $oid 本地订单ID
  16. * @property int $amount 金额,单位分
  17. * @property string|null $qr_code 支付二维码
  18. * @property string|null $url 支付链接
  19. * @property int $status 状态:-1-支付失败、0-等待支付、1-支付成功
  20. * @property Carbon $created_at
  21. * @property Carbon $updated_at
  22. * @property-read mixed $status_label
  23. * @property-read Order|null $order
  24. * @property-read User $user
  25. * @method static Builder|Payment newModelQuery()
  26. * @method static Builder|Payment newQuery()
  27. * @method static Builder|Payment query()
  28. * @method static Builder|Payment uid()
  29. * @method static Builder|Payment whereAmount($value)
  30. * @method static Builder|Payment whereCreatedAt($value)
  31. * @method static Builder|Payment whereId($value)
  32. * @method static Builder|Payment whereOid($value)
  33. * @method static Builder|Payment whereQrCode($value)
  34. * @method static Builder|Payment whereSn($value)
  35. * @method static Builder|Payment whereStatus($value)
  36. * @method static Builder|Payment whereUpdatedAt($value)
  37. * @method static Builder|Payment whereUrl($value)
  38. * @method static Builder|Payment whereUserId($value)
  39. * @mixin Eloquent
  40. */
  41. class Payment extends Model
  42. {
  43. protected $table = 'payment';
  44. protected $primaryKey = 'id';
  45. protected $appends = ['status_label'];
  46. function scopeUid($query)
  47. {
  48. return $query->whereUserId(Auth::user()->id);
  49. }
  50. function user()
  51. {
  52. return $this->belongsTo(User::class, 'user_id', 'id');
  53. }
  54. function order()
  55. {
  56. return $this->belongsTo(Order::class, 'oid', 'oid');
  57. }
  58. function getAmountAttribute($value)
  59. {
  60. return $value/100;
  61. }
  62. function setAmountAttribute($value)
  63. {
  64. return $this->attributes['amount'] = $value*100;
  65. }
  66. // 订单状态
  67. function getStatusLabelAttribute()
  68. {
  69. switch($this->attributes['status']){
  70. case -1:
  71. $status_label = '支付失败';
  72. break;
  73. case 1:
  74. $status_label = '支付成功';
  75. break;
  76. case 0:
  77. default:
  78. $status_label = '等待支付';
  79. }
  80. return $status_label;
  81. }
  82. }