Coupon.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. /**
  6. * 优惠券.
  7. */
  8. class Coupon extends Model
  9. {
  10. use SoftDeletes;
  11. protected $table = 'coupon';
  12. protected $casts = ['limit' => 'array', 'start_time' => 'date:Y-m-d', 'end_time' => 'date:Y-m-d'];
  13. protected $dates = ['deleted_at'];
  14. protected $guarded = [];
  15. // 筛选类型
  16. public function scopeType($query, $type)
  17. {
  18. return $query->whereType($type);
  19. }
  20. public function setStartTimeAttribute($value)
  21. {
  22. return $this->attributes['start_time'] = strtotime($value);
  23. }
  24. public function setEndTimeAttribute($value)
  25. {
  26. return $this->attributes['end_time'] = strtotime($value);
  27. }
  28. public function used()
  29. {
  30. $this->attributes['status'] = 1;
  31. return $this->save();
  32. }
  33. public function expired()
  34. {
  35. $this->attributes['status'] = 2;
  36. return $this->save();
  37. }
  38. public function isExpired()
  39. {
  40. return $this->attributes['end_time'] < time() || $this->attributes['status'] === 2;
  41. }
  42. }