Pārlūkot izejas kodu

Test update value

Azam Rezayi 4 gadi atpakaļ
vecāks
revīzija
51cfc98989

+ 14 - 11
packages/product/src/Http/Controllers/ProductController.php

@@ -13,7 +13,7 @@ use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Str;
 use Packages\Product\Http\Requests\AttributableRequest;
 use Packages\Product\Http\Requests\AttributeRequest;
-use Packages\Product\Models\attributables;
+use Packages\Product\Models\AttributeProduct;
 use Packages\Product\Models\Attribute;
 use Packages\Product\Models\Draft;
 use Packages\Product\Http\Requests\ProductRequest;
@@ -207,17 +207,20 @@ class ProductController extends Controller
 
         foreach ( $request->val_attribute as $attr_id => $values){
             foreach ( $values as $value ){
-                echo $attr_id . $value;
-                die();
+                $data = [
+                    'val_attribute' => $value,
+                    'attribute_id'=>  $attr_id,
+                    '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)) {
+                    $attributeProducts = AttributeProduct::create($data);
+                } else {
+                    $attribute_products->update($data);
+                }
+
             }
-            $data = [
-                'val_attribute' => [$request->val_attribute],
-                'attribute_id'=>  $attr_id,
-                'attributable_id' => $request->productID,
-                'attributable_type' => 'Packages\Product\Models\Product',
-            ];
-           dd($data);
-            $attributables =Attributables::create($data);
 
         }
     }

+ 0 - 13
packages/product/src/Models/Attributables.php

@@ -1,13 +0,0 @@
-<?php
-
-namespace Packages\Product\Models;
-
-use Illuminate\Database\Eloquent\Model;
-use Illuminate\Database\Eloquent\SoftDeletes;
-
-class AttributeJoinProduct extends Model
-{
-    use SoftDeletes;
-    protected $table = 'attributables';
-    protected $guarded = [];
-}

+ 38 - 0
packages/product/src/Models/AttributeProduct.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace Packages\Product\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class AttributeProduct extends Model
+{
+    use SoftDeletes;
+    protected $table = 'attribute_products';
+    protected $guarded = [];
+    public function syncing($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;
+    }
+}
+

+ 4 - 5
packages/product/src/database/migrations/2020_08_12_070412_create_attributable_table.php → packages/product/src/database/migrations/2020_08_22_125642_create_attribute_products_table.php

@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Support\Facades\Schema;
 
-class CreateAttributableTable extends Migration
+class CreateAttributeProductsTable extends Migration
 {
     /**
      * Run the migrations.
@@ -13,14 +13,13 @@ class CreateAttributableTable extends Migration
      */
     public function up()
     {
-        Schema::create('attributables', function (Blueprint $table) {
-            $table->increments('id');
+        Schema::create('attribute_products', function (Blueprint $table) {
+            $table->id();
             $table->integer('attribute_id');
             $table->integer('attributable_id');
             $table->string('attributable_type');
             $table->string('val_attribute')->nullable();
             $table->timestamps();
-            $table->softDeletes();
         });
     }
 
@@ -31,6 +30,6 @@ class CreateAttributableTable extends Migration
      */
     public function down()
     {
-        Schema::dropIfExists('attributables');
+        Schema::dropIfExists('attribute_products');
     }
 }

+ 31 - 28
packages/product/src/js/product.js

@@ -30,38 +30,41 @@ $(document).on('click', '.attribute_creator', function () {
             tags: true
         });
     }
-    //*********save attribute by ajax
-    $(".saveItemAttribute").on('click',function(event){
-        event.preventDefault();
-        var loc = window.location;
-        var base_url = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "");
-        var data = new FormData();
-        data.append('_token', $('[name="csrf-token"]').attr('content'));
-        $('select.newSelectItem').each(function(){
-            let attribute_values = $(this).val();
-            for( let i=0; i<attribute_values.length; i++ ){
-                data.append( $(this).attr('name'), attribute_values[i]);
+});
 
+//*********save attribute by ajax
+$("#saveItemAttribute").on('click',function(event){
+    event.preventDefault();
+    var loc = window.location;
+    var base_url = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "");
+    var data = new FormData();
+    data.append('_token', $('[name="csrf-token"]').attr('content'));
+    console.log('2');
+    $('.newSelectItem').each(function(){
+        console.log('3');
+        let attribute_values = $(this).val();
+        for( let i=0; i<attribute_values.length; i++ ){
+            data.append( $(this).attr('name'), attribute_values[i]);
+        }
+    });
+    data.append('productID',$('#productID').val());
+    $.ajax({
+        url:  base_url + "/panel/attributes_uploads/val_attribute",
+        method: 'POST',
+        data: data,
+        processData: false,
+        contentType: false,
+        success: function(operate) {
+            if (operate.status == 200){
+                swal(operate.msg);
+            }else {
+                swal(operate.msg);
             }
-        });
-        data.append('productID',$('#productID').val());
-        $.ajax({
-            url:  base_url + "/panel/attributes_uploads/val_attribute",
-            method: 'POST',
-            data: data,
-            processData: false,
-            contentType: false,
-            success: function(operate) {
-                if (operate.status == 200){
-                    swal(operate.msg);
-                }else {
-                    swal(operate.msg);
-                }
-            }
-        });
-
+        }
     });
+
 });
+
 //******add product variable
 $(document).on('click', '.variable_creator', function () {
     $(".added_variable:last").after($(".added_variable:last").clone());