Payment.php 2.5 KB

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