diff --git a/app/Listeners/ManageProductImages.php b/app/Listeners/ManageProductImages.php
index f5ffa0bc..8983d1f8 100644
--- a/app/Listeners/ManageProductImages.php
+++ b/app/Listeners/ManageProductImages.php
@@ -25,10 +25,11 @@ public function __construct()
*/
public function handle($event)
{
- $images = [ $event->data['base_image'] => ['img_type' => 'base'] ];
- foreach($event->data['additional_images'] ?? [] as $additional_image) {
+ $order = 0;
+ $images = [$event->data['base_image'] => ['img_type' => 'base']];
+ foreach ($event->data['additional_images'] ?? [] as $additional_image) {
$additional_image != $event->data['base_image'] && (
- $images[$additional_image] = ['img_type' => 'additional']
+ $images[$additional_image] = ['img_type' => 'additional', 'order' => ++$order]
);
}
diff --git a/app/Product.php b/app/Product.php
index 946ab4e4..edd8163c 100644
--- a/app/Product.php
+++ b/app/Product.php
@@ -114,7 +114,8 @@ public function brand()
public function images()
{
return $this->belongsToMany(Image::class)
- ->withPivot('img_type')
+ ->withPivot(['img_type', 'order'])
+ ->orderBy('order')
->withTimestamps();
}
diff --git a/database/migrations/2024_04_21_153831_add_order_column_to_image_product_table.php b/database/migrations/2024_04_21_153831_add_order_column_to_image_product_table.php
new file mode 100644
index 00000000..17231095
--- /dev/null
+++ b/database/migrations/2024_04_21_153831_add_order_column_to_image_product_table.php
@@ -0,0 +1,32 @@
+integer('order')->default(0)->after('img_type');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('image_product', function (Blueprint $table) {
+ $table->dropColumn('order');
+ });
+ }
+}
diff --git a/resources/views/admin/products/edit.blade.php b/resources/views/admin/products/edit.blade.php
index bec7be0c..6399efd1 100644
--- a/resources/views/admin/products/edit.blade.php
+++ b/resources/views/admin/products/edit.blade.php
@@ -18,6 +18,7 @@
@push('styles')
+