Payment.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 int $status 状态:-1-支付失败、0-等待支付、1-支付成功
  19. * @property Carbon $created_at
  20. * @property Carbon $updated_at
  21. * @property-read mixed $status_label
  22. * @property-read Order|null $order
  23. * @property-read User $user
  24. * @method static Builder|Payment newModelQuery()
  25. * @method static Builder|Payment newQuery()
  26. * @method static Builder|Payment query()
  27. * @method static Builder|Payment uid()
  28. * @method static Builder|Payment whereAmount($value)
  29. * @method static Builder|Payment whereCreatedAt($value)
  30. * @method static Builder|Payment whereId($value)
  31. * @method static Builder|Payment whereOid($value)
  32. * @method static Builder|Payment whereQrCode($value)
  33. * @method static Builder|Payment whereSn($value)
  34. * @method static Builder|Payment whereStatus($value)
  35. * @method static Builder|Payment whereUpdatedAt($value)
  36. * @method static Builder|Payment whereUserId($value)
  37. * @mixin Eloquent
  38. */
  39. class Payment extends Model
  40. {
  41. protected $table = 'payment';
  42. protected $primaryKey = 'id';
  43. protected $appends = ['status_label'];
  44. function scopeUid($query)
  45. {
  46. return $query->whereUserId(Auth::user()->id);
  47. }
  48. function user()
  49. {
  50. return $this->belongsTo(User::class, 'user_id', 'id');
  51. }
  52. function order()
  53. {
  54. return $this->belongsTo(Order::class, 'oid', 'oid');
  55. }
  56. function getAmountAttribute($value)
  57. {
  58. return $value/100;
  59. }
  60. function setAmountAttribute($value)
  61. {
  62. return $this->attributes['amount'] = $value*100;
  63. }
  64. // 订单状态
  65. function getStatusLabelAttribute()
  66. {
  67. switch($this->attributes['status']){
  68. case -1:
  69. $status_label = '支付失败';
  70. break;
  71. case 1:
  72. $status_label = '支付成功';
  73. break;
  74. case 0:
  75. default:
  76. $status_label = '等待支付';
  77. }
  78. return $status_label;
  79. }
  80. }