Payment.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. *
  11. * @property int $id
  12. * @property string|null $trade_no 支付单号(本地订单号)
  13. * @property int $user_id 用户ID
  14. * @property int|null $oid 本地订单ID
  15. * @property int $amount 金额,单位分
  16. * @property string|null $qr_code 支付二维码
  17. * @property string|null $url 支付链接
  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 whereStatus($value)
  34. * @method static Builder|Payment whereTradeNo($value)
  35. * @method static Builder|Payment whereUpdatedAt($value)
  36. * @method static Builder|Payment whereUrl($value)
  37. * @method static Builder|Payment whereUserId($value)
  38. * @mixin Eloquent
  39. */
  40. class Payment extends Model {
  41. protected $table = 'payment';
  42. protected $primaryKey = 'id';
  43. protected $appends = ['status_label'];
  44. function scopeUid($query) {
  45. return $query->whereUserId(Auth::user()->id);
  46. }
  47. function user() {
  48. return $this->belongsTo(User::class, 'user_id', 'id');
  49. }
  50. function order() {
  51. return $this->belongsTo(Order::class, 'oid', 'oid');
  52. }
  53. function getAmountAttribute($value) {
  54. return $value / 100;
  55. }
  56. function setAmountAttribute($value) {
  57. return $this->attributes['amount'] = $value * 100;
  58. }
  59. // 订单状态
  60. function getStatusLabelAttribute() {
  61. switch($this->attributes['status']){
  62. case -1:
  63. $status_label = '支付失败';
  64. break;
  65. case 1:
  66. $status_label = '支付成功';
  67. break;
  68. case 0:
  69. default:
  70. $status_label = '等待支付';
  71. }
  72. return $status_label;
  73. }
  74. }