Browse Source

End iserte input value

Azam Rezayi 4 years ago
parent
commit
c37bdeacab

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

@@ -11,7 +11,7 @@ use Illuminate\Support\Facades\Facade;
 use Illuminate\Support\Facades\File;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Str;
-use Packages\Product\Http\Requests\AttributableRequest;
+use Packages\Product\Http\Requests\AttributproductRequest;
 use Packages\Product\Http\Requests\AttributeRequest;
 use Packages\Product\Models\AttributeProduct;
 use Packages\Product\Models\Attribute;
@@ -200,32 +200,24 @@ class ProductController extends Controller
         return redirect()->back();
     }
 //    ****************************                                                                                         ********************
-    public function addValAttribute(AttributableRequest $request )
+    public function syncAttribute(AttributProductRequest $request )
     {
         $productID = $request->productID;
         $product = Product::where('id', $productID)->first();
-
+        $attribute_products = AttributeProduct::where('product_id',$request->productID)->get();
+        foreach($attribute_products as $deleteAP){
+            $deleteAP->delete();
+        }
         foreach ( $request->val_attribute as $attr_id => $values){
             foreach ( $values as $value ){
                 $data = [
                     'val_attribute' => $value,
-                    'attribute_id'=>  $attr_id,
-                    'attributable_id' => $request->productID,
-                    'attributable_type' => 'Packages\Product\Models\Product',
+                    'attribute_id' => $attr_id,
+                    'product_id' => $request->productID,
                 ];
-           $attribute_products = AttributeProduct::where('val_attribute',$value)->where('attribute_id',$attr_id)->first();
-
-             if ($attribute_products == false)  {
-                    $attributeProducts = AttributeProduct::create($data);
-//                 $attribute_products->delete();
-              } else {
-
-                $attribute_products->update($data);
-
-              }
-
-            }
-
+              $attribute_products = AttributeProduct::create($data);
+           }
         }
     }
+
 }

+ 1 - 1
packages/product/src/Http/Requests/AttributableRequest.php → packages/product/src/Http/Requests/AttributProductRequest.php

@@ -4,7 +4,7 @@ namespace Packages\Product\Http\Requests;
 use Illuminate\Foundation\Http\FormRequest;
 use Illuminate\Validation\Rule;
 
-class AttributableRequest extends FormRequest
+class AttributproductRequest extends FormRequest
 {
     /**
      * Determine if the user is authorized to make this request.

+ 14 - 31
packages/product/src/Models/AttributeProduct.php

@@ -3,44 +3,27 @@
 namespace Packages\Product\Models;
 
 use Illuminate\Database\Eloquent\Model;
-use Illuminate\Database\Eloquent\SoftDeletes;
 
 class AttributeProduct extends Model
 {
-    use SoftDeletes;
+    public $timestamps = false;
     protected $table = 'attribute_products';
-    protected $guarded = [];
-    public static function sync_attr(array $attributes = [])
-    {
-        $model = new static($attributes);
-
-        $model->save();
-
-        return $model;
-    }
-//    public function sync_attr($ids, $detaching = true)
+    protected $fillable = ['attribute_id', 'product_id', 'val_attribute'];
+//    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);
+//        $changes_attribute_products = new static($attributes);
+//        if (!$changes_attribute_products ){
+//            $changes_attribute_products->save();
 //
-//            $changes['detached'] = $this->castKeys($detach);
-//        }
-//        $changes = array_merge(
-//            $changes, $this->attachNew($records, $current, false)
-//        );
-//        if (count($changes['attached']) ||
-//            count($changes['updated'])) {
-//            $this->touchIfTouching();
+//        }else {
+//            $changes_attribute_products->delete();
+//            $changes_attribute_products->update();
+////
 //        }
-//        return $changes;
+
+
+//        return $changes_attribute_products;
 //    }
+
 }
 

+ 2 - 5
packages/product/src/database/migrations/2020_08_22_125642_create_attribute_products_table.php

@@ -16,11 +16,8 @@ class CreateAttributeProductsTable extends Migration
         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();
+            $table->integer('product_id');
+            $table->string('val_attribute');
         });
     }
 

+ 2 - 2
packages/product/src/routes/web.php

@@ -11,8 +11,8 @@ Route::group([
     Route::get('product/product-variable/{id}', 'ProductController@productStepVariable')->name('productStepVariable');
 
 //    add variable product
-    Route::post('attributes_uploads/val_attribute', 'ProductController@addValAttribute')->name('val_attribute');
-    Route::get('product/product-variabled', 'ProductController@addValAttribute')->name('addValAttribute');
+    Route::post('attributes_uploads/val_attribute', 'ProductController@syncAttribute')->name('val_attribute');
+    Route::get('product/val_attribute', 'ProductController@syncAttribute')->name('syncAttribute');
 //upload*****************************
     Route::get('uploads/{upload}', 'ProductController@destroyFile')->name('uploads.destroyFile');
     Route::post('product_uploads/FeaturedImage', 'ProductController@uploadFeature')->name('FeaturedImage');