我是laravel框架的新手。我遇到错误
ErrorException(E_WARNING) 非法字符串偏移量'customerid'
我试图导入Excel文件,并在同一页的数据表中显示数据。我遇到错误:
调用未定义的方法maatwebsite / Excel / Excel :: load()。
要解决此错误,我将maatwebsite/excel
版本从3.1
降级到~2.1.0
,然后使用composer update
更新了作曲家。然后,我又收到另一个错误“ continue”(定位)开关,它等同于break -我通过更改OLE.php文件中的“ continuecontinue 2”来修复它。
现在我遇到了一个新错误
“ ErrorException(E_WARNING) 非法的字符串偏移量'customerid'“
public function import_csv(Request $request)
{
$this->validate($request, [
'select_file' => 'required|mimes:xls,xlsx',
]);
$path = $request->file('select_file')->getRealPath();
$data = Excel::load($path)->get();
if ($data->count() > 0) {
foreach ($data->toArray() as $key => $value) {
foreach ($value as $row) {
$insert_data[] = [
'customerid' => $row['customerid'],
'enquiryid' => $row['enquiryid'],
'productid' => $row['productid'],
'productname' => $row['productname'],
'quantity' => $row['quantity'],
];
}
}
if (!empty($insert_data)) {
DB::table('enquiryproducts')->insert($insert_data);
}
}
return back()->with('success', 'Excel Data Imported successfully.');
}
import_csv.blade.php :
<form id="upload_csv_form" action="{{ url('/import_csv/import') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<div class="col-lg-6 col-md-12">
<fieldset class="form-group">
<input type="file" name="select_file" accept=".csv" class="form-control-file" id="select_file">
</fieldset>
<button type="submit" name="upload_csv" class="btn btn-success">Upload Enquiry Products</button>
</div>
</div>
</form>
答案 0 :(得分:1)
您不必为$value
进行另一个foreach循环。
尝试一下:
if($data->count() > 0)
{
foreach($data as $key => $value)
$insert_data[] = array(
'customerid' => $value['customerid'],
'enquiryid' => $value['enquiryid'],
'productid' => $value['productid'],
'productname' => $value['productname'],
'quantity' => $value['quantity'],
);
}