Coupon.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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', 'deleted_at' => 'datetime'];
  13. protected $guarded = [];
  14. // 筛选类型
  15. public function scopeType($query, $type)
  16. {
  17. return $query->whereType($type);
  18. }
  19. public function setStartTimeAttribute($value)
  20. {
  21. return $this->attributes['start_time'] = strtotime($value);
  22. }
  23. public function setEndTimeAttribute($value)
  24. {
  25. return $this->attributes['end_time'] = strtotime($value);
  26. }
  27. public function used(): bool
  28. {
  29. $this->attributes['status'] = 1;
  30. return $this->save();
  31. }
  32. public function expired(): bool
  33. {
  34. $this->attributes['status'] = 2;
  35. return $this->save();
  36. }
  37. public function isExpired(): bool
  38. {
  39. return $this->attributes['end_time'] < time() || $this->attributes['status'] === 2;
  40. }
  41. }