Pārlūkot izejas kodu

End insert value attribute

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

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

@@ -2,6 +2,7 @@
 
 namespace Packages\Product\Http\Controllers;
 
+use Carbon\Carbon;
 use Illuminate\Support\Facades\Auth;
 use App\Http\Controllers\Controller;
 use Facade\Ignition\Support\Packagist\Package;
@@ -18,6 +19,7 @@ use Packages\Product\Models\Product;
 use Illuminate\Http\Request;
 use App\Models\Category;
 use App\Models\Upload;
+use Packages\Product\Models\Temp;
 use phpDocumentor\Reflection\Types\Compound;
 
 class ProductController extends Controller
@@ -52,7 +54,7 @@ class ProductController extends Controller
 //**************************************************** page create products *****************
     public function create(Request $request)
     {
-        return view('product::product.create',compact('attributes'));
+        return view('product::product.create');
     }
 // *********************************************************save products ******************
     public function store(ProductRequest $request)
@@ -219,19 +221,34 @@ class ProductController extends Controller
 
     public function uploadValAttribute(Request $request)
     {
-        $attributes = Attribute::orderBy('id')->where('use_in_veriation', 1)->get();
-        $product = Product::where('id')->get();
-//dd($request);
-
-            $data = [
-                'val_attribute' => $request->val_attribute,
-                'attribute_id' => $attributes->id,
-                'attributable_id' => $product->id,
-                'attributable_type' => 'products',
-
-            ];
-            $uploaded = $product->attributes()->save($data);
-            return redirect(route('productStepVariable'));
+//       $attributes = Attribute::orderBy('id')->where('use_in_veriation', 1)->get();
+        $productID = $request->productID;
+        $product = Product::where('id', $productID)->first();
+        $info = json_decode($request->info);
+        $type = json_decode($request->type);
+        $attributables = $product->attributes()->where($info,'val_attribute');
+//        dd($attributables);
+        dd($type);
+        if (($attributables)){
+            foreach ($info as $item){
+                $data = [
+                    'val_attribute' => $item,
+                    'attribute_id' => $type,
+                    'attributable_id' => $productID,
+                    'attributable_type' => 'products',
+                    'created_at' => Carbon::now(),
+                    'updated_at' => Carbon::now(),
+                ];
+
+                \DB::table('attributables')->insert($data);
+            }
+            $operate['status'] = 200;
+            $operate['msg'] = 'با موفقیت ذخیره شد !';
+        }else{
+            $operate['status'] = 500;
+            $operate['msg'] = 'اطلاعات قبلا ذخیره شده';
+        }
+            return $operate;
         }
 
 }

+ 1 - 0
packages/product/src/Models/Attribute.php

@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model;
 class Attribute extends Model
 {
     use SoftDeletes;
+    protected $table = 'attributes';
     protected $fillable=['title', 'key', 'use_in_veriation'];
     public function products()
     {

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

@@ -16,7 +16,7 @@ class CreateAttributesTable extends Migration
         Schema::create('attributes', function (Blueprint $table) {
             $table->id();
             $table->string('title');
-            $table->boolean('use_in_veriation')->unique()->default(true);
+            $table->boolean('use_in_veriation')->default(true);
             $table->string('key');
             $table->softDeletes();
             $table->timestamps();

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

@@ -13,12 +13,12 @@ class CreateAttributableTable extends Migration
      */
     public function up()
     {
-        Schema::create('attributable', function (Blueprint $table) {
-
+        Schema::create('attributables', function (Blueprint $table) {
+            $table->increments('id');
             $table->integer('attribute_id');
             $table->integer('attributable_id');
             $table->string('attributable_type');
-            $table->string('val_attribute');
+            $table->string('val_attribute')->nullable();
             $table->timestamps();
             $table->softDeletes();
         });
@@ -31,6 +31,6 @@ class CreateAttributableTable extends Migration
      */
     public function down()
     {
-        Schema::dropIfExists('attributable');
+        Schema::dropIfExists('attributables');
     }
 }

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

@@ -1,5 +1,9 @@
+
+
 // Add Attribute
 $(document).on('click', '.attribute_creator', function () {
+    // const attr_id = $('#Show_attributeValue').val();
+    //  const attr_label = $('#Show_attributeValue').find('option:selected').text();
     var attr = document.getElementById("Show_attributeValue").value;
     if(attr == $('#Show_selectValue').html()){
         create = false;
@@ -20,43 +24,44 @@ $(document).on('click', '.attribute_creator', function () {
             .clone());
         new_select.find('span').remove();
         new_select.find('select').removeClass();
+        new_select.find('select').addClass('newSelectItem');
         $('div.added_attribute:last').after( new_select.html() );
         $('div.added_attribute:last').show();
         $(".added_attribute select").select2({
             tags: true
         });
     }
-
-});
-//*********save attribute by ajax
-$(document).ready(function(){
-    $(".attachment_value_attribute").on('change',function(){
+    //*********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 : "");
-        for( let i=0; i<$(this).prop('files').length; i++ ){
-            var uploader = $('.attachment_value_attribute[name="val_attribute[]"]');
-            var data = new FormData();
-            uploader.each(function(){
-                data.append('val_attribute[]', $('#val_attribute').val());
-            })
-            data.append('_token', $('[name="csrf-token"]').attr('content'));
-            $.ajax({
-                url:  base_url + "/panel/product_uploads/uploadValAttribute",
-                method: 'POST',
-                data: data,
-                processData: false,
-                contentType: false,
-                success: function(data) {
-
+        var data = new FormData();
+        data.append('_token', $('[name="csrf-token"]').attr('content'));
+        data.append('info', JSON.stringify($('.newSelectItem').val()));
+        data.append('type', JSON.stringify($('#Show_attributeValue').val()));
+        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());
-    $("div.added_variable:last").addClass("added_variable_show");
+    $("div.added_variable:last").show();
 });
 
 

+ 4 - 1
packages/product/src/routes/web.php

@@ -11,6 +11,9 @@ Route::group([
     Route::get('product/product-variable/{id}', 'ProductController@productStepVariable')->name('productStepVariable');
 
 
+
+    Route::post('attributes_uploads/val_attribute', 'ProductController@uploadValAttribute')->name('val_attribute');
+
 //upload*****************************
     Route::get('uploads/{upload}', 'ProductController@destroyFile')->name('uploads.destroyFile');
     Route::post('product_uploads/FeaturedImage', 'ProductController@uploadFeature')->name('FeaturedImage');
@@ -23,7 +26,7 @@ Route::group([
     'prefix' => 'panel'
 ], function () {
     Route::resource('attributes', 'AttributeController');
-    Route::post('attributes_uploads/val_attribute', 'ProductController@uploadValAttribute')->name('uploadValAttribute');
+
 
 });
 

+ 0 - 6
packages/product/src/sass/product.scss

@@ -169,12 +169,6 @@ output span {
    border-radius: 5px;
        margin: 10px;
  }
-.added_attribute_show {
-    display: inline !important;
-}
-.added_variable_show{
-     display: inline !important;
- }
 .btn{
     height: 3em ;
 }

+ 5 - 6
packages/product/src/views/product/include/createAttribute.blade.php

@@ -4,8 +4,7 @@
             <strong>{{ __('انتخاب ویژگی ها') }}</strong>
         </div>
         <div class="card-body mx-lg-6 px-lg-4">
-            <form method="POST" action="{{ route('uploadValAttribute') }}" enctype="multipart/form-data" >
-            @csrf
+            <input type="hidden" name="productID" id="productID" value="{{ $product->id }}">
             <div class="form-row ">
                 <div class="form-group col-lg-4">
                     <label for="status">{{ __('attribute.titleProduct') }}</label><i class="required">&nbsp; *</i>
@@ -13,7 +12,7 @@
                             class="form-control attributes @error('attributes') is-invalid @enderror"
                             name="attributes" value="{{ old('attributes') }}" autocomplete="attributes">
                         @foreach($attributes as $attr_id)
-                            <option value="{{$attr_id->title}}">{{$attr_id->title}}</option>
+                            <option value="{{$attr_id->id}}">{{$attr_id->title}}</option>
                         @endforeach
                     </select>
                     @error('ReleaseStatus')
@@ -32,19 +31,19 @@
                         <label id="Show_selectValue" class="attr"></label><per class="font-color">:</per>
                     </div>
                     <div class="form-group Attr_item">
-                    <select name="val_attribute[]" id="" class="attr js-example-tokenizer attachment_value_attribute" multiple="multiple">
+{{--                        <input name="val_attribute[]" id="" class="attr js-example-tokenizer saveItemAttribute" multiple="multiple">--}}
+                    <select name="val_attribute[]" id="" class="val_attributes_all attr js-example-tokenizer " multiple="multiple">
                         <option value=""></option>
                     </select>
                     <a href="" class="deleteGallery btn btn-danger ">
                         <i class="icon fa fa-close AddPad"></i>
                     </a>
-                    <button type="submit" class="btn btn-success ">
+                    <button type="submit" class="saveItemAttribute btn btn-success ">
                         اعمال
                     </button>
                     </div>
                 </div>
             </div>
-            </form>
         </div>
 
     </div>

+ 0 - 1
resources/lang/fa/attribute.php

@@ -5,7 +5,6 @@ return [
     'key' => ' key ',
     'use_in_veriation' => ' استفاده از محصولات متغیر ',
 
-
 ];