DistProductImage.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Distributor\Repositories;
  3. use App\Models\DistProductImage as Model;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. class DistProductImage extends EloquentRepository
  6. {
  7. /**
  8. * Model.
  9. *
  10. * @var string
  11. */
  12. protected $eloquentClass = Model::class;
  13. //格式化数据,用于多图上传
  14. public static function formatData($productId, $images)
  15. {
  16. $existingImages = Model::where('product_id', $productId)->get();
  17. // 用于存储结果的数组
  18. $result = [];
  19. // 处理条件1:如果数据在 base_product_image 中有,但在 $images 上无
  20. foreach ($existingImages as $existingImage) {
  21. if (!in_array($existingImage->image_url, $images)) {
  22. $result[] = [
  23. 'id' => $existingImage->id,
  24. 'image_url' => $existingImage->image_url,
  25. '_remove_' => 1,
  26. ];
  27. }
  28. }
  29. // 处理条件2:如果数据在 base_product_image 中无,但在 $images 有
  30. foreach ($images as $image) {
  31. $found = $existingImages->firstWhere('image_url', $image);
  32. if (!$found) {
  33. $result[] = [
  34. 'id' => 0,
  35. 'image_url' => $image,
  36. ];
  37. }
  38. }
  39. return $result;
  40. }
  41. }