Parcourir la source

inserte in to table attribute producte

Azam Rezayi il y a 4 ans
Parent
commit
22d1daf97b

+ 9 - 5
packages/product/src/Http/Controllers/ProductController.php

@@ -213,12 +213,16 @@ class ProductController extends Controller
                     'attributable_id' => $request->productID,
                     'attributable_type' => 'Packages\Product\Models\Product',
                 ];
-                $attribute_products = AttributeProduct::where('val_attribute', $value)->where('uploadable_id', $attr_id)->first();
-                if (empty($attribute_products)) {
+           $attribute_products = AttributeProduct::where('val_attribute',$value)->where('attribute_id',$attr_id)->first();
+
+             if ($attribute_products == false)  {
                     $attributeProducts = AttributeProduct::create($data);
-                } else {
-                    $attribute_products->update($data);
-                }
+//                 $attribute_products->delete();
+              } else {
+
+                $attribute_products->update($data);
+
+              }
 
             }
 

+ 29 - 21
packages/product/src/Models/AttributeProduct.php

@@ -10,29 +10,37 @@ class AttributeProduct extends Model
     use SoftDeletes;
     protected $table = 'attribute_products';
     protected $guarded = [];
-    public function syncing($ids, $detaching = true)
+    public static function sync_attr(array $attributes = [])
     {
-        $changes = [
-            'attached' => [], 'detached' => [], 'updated' => [],
-        ];
-        $current = $this->getCurrentlyAttachedPivots()
-            ->pluck($this->relatedPivotKey)->all();
-        $detach = array_diff($current, array_keys(
-            $records = $this->formatRecordsList($this->parseIds($ids))
-        ));
-        if ($detaching && count($detach) > 0) {
-            $this->detach($detach);
+        $model = new static($attributes);
 
-            $changes['detached'] = $this->castKeys($detach);
-        }
-        $changes = array_merge(
-            $changes, $this->attachNew($records, $current, false)
-        );
-        if (count($changes['attached']) ||
-            count($changes['updated'])) {
-            $this->touchIfTouching();
-        }
-        return $changes;
+        $model->save();
+
+        return $model;
     }
+//    public function sync_attr($ids, $detaching = true)
+//    {
+//        $changes = [
+//            'attached' => [], 'detached' => [], 'updated' => [],
+//        ];
+//        $current = $this->getCurrentlyAttachedPivots()
+//            ->pluck($this->relatedPivotKey)->all();
+//        $detach = array_diff($current, array_keys(
+//            $records = $this->formatRecordsList($this->parseIds($ids))
+//        ));
+//        if ($detaching && count($detach) > 0) {
+//            $this->detach($detach);
+//
+//            $changes['detached'] = $this->castKeys($detach);
+//        }
+//        $changes = array_merge(
+//            $changes, $this->attachNew($records, $current, false)
+//        );
+//        if (count($changes['attached']) ||
+//            count($changes['updated'])) {
+//            $this->touchIfTouching();
+//        }
+//        return $changes;
+//    }
 }
 

+ 1 - 0
packages/product/src/database/migrations/2020_08_22_125642_create_attribute_products_table.php

@@ -20,6 +20,7 @@ class CreateAttributeProductsTable extends Migration
             $table->string('attributable_type');
             $table->string('val_attribute')->nullable();
             $table->timestamps();
+            $table->softDeletes();
         });
     }