我的控制器中有一个函数,它使用php excel从excel文件中读取行,并将其存储到db表中。
\*
这是我的表
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;
}
}
当我通过foreach将其打印好的东西打印到屏幕上时没有错误,但是当我查看表时,它只有db表中数组的最后一个元素。
答案 0 :(得分:3)
如果Hotel
类是您的模型,那么您需要在foreach
块
foreach ($sheetData as $row)
{
if ($this->isDate($row[$date_col]))
{
$hotel_table = new Hotel($table);
...
}
}