DeleteExpiredCoupons.php 929 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Settings\CouponSettings;
  4. use App\Models\Coupon;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. class DeleteExpiredCoupons extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'coupons:delete';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Delete expired coupons from DB.';
  21. /**
  22. * Execute the console command.
  23. *
  24. * @return int
  25. */
  26. public function handle(CouponSettings $couponSettings)
  27. {
  28. if ($couponSettings->delete_coupon_on_expires) {
  29. $expired_coupons = Coupon::where('expires_at', '<=', Carbon::now(config('app.timezone')))->get();
  30. foreach ($expired_coupons as $expired_coupon) {
  31. $expired_coupon->delete();
  32. }
  33. }
  34. }
  35. }