doutenggu4070 2016-02-02 02:03
浏览 155
已采纳

laravel foreach循环只存储db表中数组的最后一个元素

i have function in my controller that uses php excel to read rows from excel file, and store it to db table.

public function processData(Request $request)
{
    /* store data from form in to variables */
    $hotel = $request->input('hotel');
    $date = $request->input('date');
    $start_row = $request->input('row');
    $date_col = $request->input('date-col');
    $sold_col = $request->input('sold-col');
    $rev_col = $request->input('rev-col');

    /* get tables name */
    $table = str_replace('-', '_', $hotel);
    $monthly_table = $table . '_monthly';

    /* init hotel class */
    $hotel_table = new Hotel($table);

    $file = public_path().'/'.$date.'.xls';

    $sheetData = Excel::load($file)->noHeading()->getExcel()->getSheet()->toArray(null,false,false, true);
    $sheetData = array_slice($sheetData, $start_row - 1);

    foreach ($sheetData as $row)
    {
        if ($this->isDate($row[$date_col]))
        {
            print $row[$date_col] . "---" . $row[$sold_col] . "---" . gettype(money_format('%i', floatval($row[$rev_col]))) . "<br>";
            $hotel_table->date = $row[$date_col];
            $hotel_table->sold = intval($row[$sold_col]);
            $hotel_table->sold_diff = 0;
            $hotel_table->rev = floatval($row[$rev_col]);
            $hotel_table->rev_diff = 0;
            $hotel_table->row = $start_row;
            $hotel_table->date_col = $date_col;
            $hotel_table->sold_col = $sold_col;
            $hotel_table->rev_col = $rev_col;
            //$hotel_table->save();
            if (!$hotel_table->save())
            {
                dd ( DB::getQueryLog() );
            }

        }
    }

}

public function isDate($date)
{
    if (date('m/d/Y', strtotime($date)) == $date)
    {
        return true;
    }
    else
    {
        return false;
    }
}

this is my table

Schema::create($tableName, function (Blueprint $table) {
            $table->increments('id');
            $table->string('date');
            $table->integer('sold');
            $table->integer('sold_diff');
            $table->float('rev');
            $table->float('rev_diff');
            $table->string('row');
            $table->string('date_col');
            $table->string('sold_col');
            $table->string('rev_col');
            $table->timestamps();
        });

when i go thru the foreach its printing things just fine to the screen there are no errors, but when i look at the table it only has last element of the array in db table.

  • 写回答

1条回答 默认 最新

  • duanjiagu0655 2016-02-02 02:09
    关注

    If Hotel class is your model then you need to move following code inside foreach block

    foreach ($sheetData as $row)
    {
        if ($this->isDate($row[$date_col]))
        {           
           $hotel_table = new Hotel($table);
           ...
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题