ProductFlashDealService.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Services;
  3. use App\Models\FlashDeal;
  4. use App\Models\FlashDealProduct;
  5. use App\Models\Product;
  6. class ProductFlashDealService
  7. {
  8. public function store(array $data, Product $product)
  9. {
  10. $collection = collect($data);
  11. if ($collection['flash_deal_id']) {
  12. $flash_deal_product = FlashDealProduct::firstOrNew([
  13. 'flash_deal_id' => $collection['flash_deal_id'],
  14. 'product_id' => $product->id]
  15. );
  16. $flash_deal_product->flash_deal_id = $collection['flash_deal_id'];
  17. $flash_deal_product->product_id = $product->id;
  18. $flash_deal_product->save();
  19. $flash_deal = FlashDeal::findOrFail($collection['flash_deal_id']);
  20. $product->discount = $collection['flash_discount'];
  21. $product->discount_type = $collection['flash_discount_type'];
  22. $product->discount_start_date = $flash_deal->start_date;
  23. $product->discount_end_date = $flash_deal->end_date;
  24. $product->save();
  25. }
  26. }
  27. }