(非法字符串偏移量)用于将数据导入数据库
(Illegal string offset) for import data to database
我正在尝试将 excel 导入我的数据库 table 'barangs',但它有一个错误 "Illegal string offset 'kode_barang'"。我不知道如何修复此错误。
使用 laravel maatwebsite
将数据从 excel 导入数据库
我的控制器
public function import(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[] = array(
'kodeBarang' => $row['kode_barang'],
'namaBarang' => $row['nama_barang'],
'stock' => $row['stock'],
'hargaJual' => $row['harga_jual'],
'kategory' => $row['kategory']
);
}
}
if (!empty($insert_data)) {
barang::table('barangs')->insert($insert_data);
}
}
return back()->with('success','berhasil di upload');
}
我收到这样的错误消息
Illegal string offset 'kode_barang'
in barangController.php line 55 at HandleExceptions->handleError(2,
'Illegal string offset \'kode_barang\'',
'C:\xampp\htdocs\penjualan\app\Http\Controllers\barangController.php',
55, array('request' => object(Request), 'path' =>
'C:\xampp\tmp\phpE998.tmp', 'data' => object(RowCollection), 'key'
=> 0, 'value' => array('kode_barang' => 331211, 'nama_barang' => 'coba import', 'stock' => 2, 'harga_jual' => 3000, 'kategory' => 'Minuman',
null), 'row' => 'coba import', 'insert_data' =>
array(array('kodeBarang' => null, 'namaBarang' => null, 'stock' =>
null, 'hargaJual' => null, 'kategory' => null))))
您可以使用以下代码解决此问题。
foreach ($data->toArray() as $key => $value) {
foreach ($value as $row_key => $row) {
$insert_data[] = array(
'kodeBarang' => $row[0],
'namaBarang' => $row[1],
'stock' => $row[2],
'hargaJual' => $row[3],
'kategory' => $row[4]
);
}
}
添加条件以检查 null
行。
foreach ($data->toArray() as $key => $value) {
foreach ($value as $row_key => $row) {
if(!isset($row[0])) {
$insert_data[] = array(
'kodeBarang' => $row[0],
'namaBarang' => $row[1],
'stock' => $row[2],
'hargaJual' => $row[3],
'kategory' => $row[4]
);
}
}
}
或
foreach ($data->toArray() as $key => $value) {
foreach ($value as $row_key => $row) {
if(!isset($row['kode_barang'])) {
$insert_data[] = array(
'kodeBarang' => $row['kode_barang'],
'namaBarang' => $row['nama_barang'],
'stock' => $row['stock'],
'hargaJual' => $row['harga_jual'],
'kategory' => $row['kategory']
);
}
}
}
我正在尝试将 excel 导入我的数据库 table 'barangs',但它有一个错误 "Illegal string offset 'kode_barang'"。我不知道如何修复此错误。
使用 laravel maatwebsite
将数据从 excel 导入数据库我的控制器
public function import(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[] = array(
'kodeBarang' => $row['kode_barang'],
'namaBarang' => $row['nama_barang'],
'stock' => $row['stock'],
'hargaJual' => $row['harga_jual'],
'kategory' => $row['kategory']
);
}
}
if (!empty($insert_data)) {
barang::table('barangs')->insert($insert_data);
}
}
return back()->with('success','berhasil di upload');
}
我收到这样的错误消息
Illegal string offset 'kode_barang'
in barangController.php line 55 at HandleExceptions->handleError(2, 'Illegal string offset \'kode_barang\'', 'C:\xampp\htdocs\penjualan\app\Http\Controllers\barangController.php', 55, array('request' => object(Request), 'path' => 'C:\xampp\tmp\phpE998.tmp', 'data' => object(RowCollection), 'key' => 0, 'value' => array('kode_barang' => 331211, 'nama_barang' => 'coba import', 'stock' => 2, 'harga_jual' => 3000, 'kategory' => 'Minuman', null), 'row' => 'coba import', 'insert_data' => array(array('kodeBarang' => null, 'namaBarang' => null, 'stock' => null, 'hargaJual' => null, 'kategory' => null))))
您可以使用以下代码解决此问题。
foreach ($data->toArray() as $key => $value) {
foreach ($value as $row_key => $row) {
$insert_data[] = array(
'kodeBarang' => $row[0],
'namaBarang' => $row[1],
'stock' => $row[2],
'hargaJual' => $row[3],
'kategory' => $row[4]
);
}
}
添加条件以检查 null
行。
foreach ($data->toArray() as $key => $value) {
foreach ($value as $row_key => $row) {
if(!isset($row[0])) {
$insert_data[] = array(
'kodeBarang' => $row[0],
'namaBarang' => $row[1],
'stock' => $row[2],
'hargaJual' => $row[3],
'kategory' => $row[4]
);
}
}
}
或
foreach ($data->toArray() as $key => $value) {
foreach ($value as $row_key => $row) {
if(!isset($row['kode_barang'])) {
$insert_data[] = array(
'kodeBarang' => $row['kode_barang'],
'namaBarang' => $row['nama_barang'],
'stock' => $row['stock'],
'hargaJual' => $row['harga_jual'],
'kategory' => $row['kategory']
);
}
}
}