BaseProductImage.php 993 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\BaseProductImage as Model;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. use Carbon\Carbon;
  6. class BaseProductImage extends EloquentRepository
  7. {
  8. /**
  9. * Model.
  10. *
  11. * @var string
  12. */
  13. protected $eloquentClass = Model::class;
  14. public static function deleteByProductId($productId)
  15. {
  16. Model::where('product_id', $productId)->delete();
  17. }
  18. public static function saveProductImages($productId, $imageUrls)
  19. {
  20. if (empty($productId) || empty($imageUrl)) {
  21. return false;
  22. }
  23. // 准备数据集合
  24. $data = [];
  25. foreach ($imageUrls as $imageUrl) {
  26. $data[] = [
  27. 'product_id' => $productId,
  28. 'image_url' => $imageUrl,
  29. 'created_at' => Carbon::now(),
  30. 'updated_at' => Carbon::now(),
  31. ];
  32. }
  33. // 批量插入
  34. return Model::insert($data);
  35. }
  36. }