skip to Main Content

I am building the e-commerce application, I have struggled to this problem when save many of variants:

Supposedly, I got a request return the collection:

let data = {     
    "_token"    :"{{ csrf_token() }}",     
    "data"  : tableToJson($(".fixed-table table"), $(".scroll-table table")), // return data
}
let res = $.post(url, data); 

It will result like this (in this case, I using with variant with 2 maximum options):

data = {
{
    color: black,
    size: 10,
    sku: abxc,
    quantity: 1,
    price: 100$,
    
},
{
    color: blue,
    size: 10,
    sku: abxz,
    quantity: 2,
    price: 1100$,
},
,....}

I intended to insert in controller with the function:

function insertVariant(Request $request) {
    $datas = $request->all();

    $id = options->insertGetId(
            [
                'name'=>'$data['color'] ,
            ]
            , 
            [
                'name'=>'$data['size'] ,
            ]
        ]);

    foreach ($datas in $data) {
        $options = new Options();

        $sku = new SKU();
        $sku['name'] = $data['sku'],
        $sku['price'] = $data['price'],
        $sku['quantity'] = $data['quatity']
        $sku->save();
        
        $variant_value = new VariantValue();
        $variant_value['variant_options_id'] = $id[0];
        $variant_value['value'] = $data['color'];
        $variant_value['value'] = $data['size'];
        $variant_value->save();
        
        $sku_variant_value = new SKUVariantValues();
        $sku_variant_value['variant_value_id'] = $variant_value->id;
        $sku_variant_value['sku_id'] = $sku->id;
        $sku_variant_value->save();
    }    
}

I see that request return too long as though its success?

how can I optimize when save multiple model in foreach with less cost as less as possible?

2

Answers


  1. You are inserting data for every iteration its may take some time to store every insertsertion. First, need to prepare data in a single array. Then insert the data outside loop.

    $skuArray = []
    
    
    foreach ($datas in $data) {    
          $sku = [
             'name' => $data['sku'],
             'price' => $data['price'],
             'quantity' => $data['quantity'],
          ];
    
         $skuArray[] = $sku;
    }
    
    SKU::insert($skuArray ); 
    
    Login or Signup to reply.
  2. Assumption: I am assuming that all tables have auto-incremented id column(primary key)

    Reference is taken from : Bulk insert and get returned ids laravel

    Here is the code suggested by me:

    function insertVariant(Request $request) {
        $datas = $request->all();
    
        $id = options->insertGetId(
            [
                'name'=>'$data['color'] ,
            ]
            , 
            [
                'name'=>'$data['size'] ,
            ]
        ]);
    
        // 1- get the last id of your table SKU say $lastIdBeforeInsertionInSkuTable
        // 2- get the last id of your table VariantValue say $lastIdBeforeInsertionInVariantValueTable
    
    
        $skuArray = []
        $variantValueArr = [];
    
        foreach ($datas in $data) {    
              $sku = [
                 'name' => $data['sku'],
                 'price' => $data['price'],
                 'quantity' => $data['quantity'],
              ];
    
             $skuArray[] = $sku;
             
             //Using value1 and value 2 because the same index values got over-written, so check and fix it at your end
             $variantValue = [
                'variant_options_id' => $id[0],
                'value1' => $data['color'],
                'value2' = $data['size'],
             ];
             $variantValueArr[] = $variantValue;
        }
    
        SKU::insert($skuArray);
        VariantValue::insert($variantValueArr);
    
        $skuIds = [];
        $variantValueIds = [];
        for($i=1; $i<=count($skuArray); $i++){
            $skuIds[] = $lastIdBeforeInsertionInSkuTable+$i;
            $variantValueIds[] = $lastIdBeforeInsertionInVariantValueTable+$i;
        }
    
        $skuVariantValuesArr = [];
        foreach($skuIds as $key=>$skuId){
            $skuVariantValuesArr[] = [
                'variant_value_id' =>$variantValueIds[$key],
                'sku_id' =>$skuId
            ];
        }
        SKUVariantValues::insert($skuVariantValuesArr);
    }
    

    Note: Read the comments inside the code carefully and work accordingly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search