Browse Source

图片排序

moshaorui 3 months ago
parent
commit
41fd5e8d5d

+ 11 - 0
app/Admin/Repositories/BaseProductImage.php

@@ -57,15 +57,26 @@ class BaseProductImage extends EloquentRepository
             }
         }
         // 处理条件2:如果数据在 base_product_image 中无,但在 $images 有
+        $i = 1;
         foreach ($images as $image) {
             $found = $existingImages->firstWhere('image_url', $image);
             if (!$found) {
                 $result[] = [
                     'id' => 0,
                     'image_url' => $image,
+                    'order' => $i,
                 ];
             }
+            //更新库中排序
+            foreach ($existingImages as $existingImage) {
+                if ($existingImage->image_url == $image) {
+                    $existingImage->order = $i;
+                    $existingImage->save();
+                }
+            }
+            $i++;
         }
+
         return $result;
     }
 

+ 10 - 0
app/Distributor/Repositories/DistProductImage.php

@@ -31,14 +31,24 @@ class DistProductImage extends EloquentRepository
             }
         }
         // 处理条件2:如果数据在 base_product_image 中无,但在 $images 有
+        $i = 1;
         foreach ($images as $image) {
             $found = $existingImages->firstWhere('image_url', $image);
             if (!$found) {
                 $result[] = [
                     'id' => 0,
                     'image_url' => $image,
+                    'order' => $i,
                 ];
             }
+            //更新库中排序
+            foreach ($existingImages as $existingImage) {
+                if ($existingImage->image_url == $image) {
+                    $existingImage->order = $i;
+                    $existingImage->save();
+                }
+            }
+            $i++;
         }
         return $result;
     }

+ 1 - 1
app/Models/BaseProduct.php

@@ -37,7 +37,7 @@ class BaseProduct extends Model
     // 一对多关联
     public function images()
     {
-        return $this->hasMany(BaseProductImage::class, 'product_id','id');
+        return $this->hasMany(BaseProductImage::class, 'product_id','id')->orderBy('order', 'asc')->orderBy('id', 'asc');
     }
 
 }

+ 3 - 1
app/Models/BaseProductImage.php

@@ -16,7 +16,9 @@ class BaseProductImage extends Model
     ];
     protected $fillable = [
         'image_url',
-        'product_id'];
+        'product_id',
+        'order',
+    ];
 
     // 反向关联,属于某个产品
     public function product()

+ 1 - 1
app/Models/DistProduct.php

@@ -56,7 +56,7 @@ class DistProduct extends Model
     // 一对多关联
     public function images()
     {
-        return $this->hasMany(DistProductImage::class, 'product_id');
+        return $this->hasMany(DistProductImage::class, 'product_id')->orderBy('order', 'asc')->orderBy('id', 'asc');
     }
 
 }