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

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);
           ...
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿