[英]How fix the "llegal string offset" while uploading excel file into database
I am trying to import an Excel file into my table using the MaatWebsite package .我正在尝试使用MaatWebsite 包将 Excel 文件导入到我的表中。 But every time I get errors like these:
但每次我收到这样的错误时:
Illegal string offset 'Emp'
非法字符串偏移量 'Emp'
I am following this tutorial .我正在关注本教程。 And here is the code I have tried:
这是我尝试过的代码:
$path = $request->file('attendance_data')->getRealPath();
$data = Excel::load($path)->get();
if($data->count() > 0)
{
foreach($data->toArray() as $key => $value)
{
foreach($value as $row)
{
$insert_data[] = array(
'employee_card' => $row['Emp'],
'attendance_date' => $row['Date'],
'attendance_time' => $row['On'],
);
}
}
if(!empty($insert_data))
{
DB::table('attendance_logs')->insert($insert_data);
}
Any help will be appreciated任何帮助将不胜感激
I can't comment so I've posted this as an answer.我无法发表评论,所以我已将此作为答案发布。
Have you tried to die and dump the $row
to check that it is an array of key => value
pairs as you're expecting?您是否尝试过死并转储
$row
以检查它是否是您期望的key => value
对数组? The error looks to show up when trying to access a key of an array with numeric keys.尝试使用数字键访问数组的键时似乎会出现该错误。 This could also happen if
$row
is a string, as you can access string characters using a numeric index similar to an array.如果
$row
是字符串,也可能发生这种情况,因为您可以使用类似于数组的数字索引访问字符串字符。
$path = $request->file('attendance_data')->getRealPath();
$data = Excel::load($path)->get();
if($data->count() > 0) {
foreach ($data->toArray() as $key => $value) {
foreach ($value as $row) {
// Print the data of $row to see what it actually is
// and kill the process
dd($row);
$insert_data[] = array(
'employee_card' => $row['Emp'],
'attendance_date' => $row['Date'],
'attendance_time' => $row['On'],
);
}
}
if (!empty($insert_data)) {
DB::table('attendance_logs')->insert($insert_data);
}
}
This answer looks to be related.这个答案看起来是相关的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.