[英]How to update data in database using import excel
how to update data in database with import excel.如何使用导入excel更新数据库中的数据。 i am using laravel 5.7 and maatwebsite 3.1我正在使用 laravel 5.7 和 maatwebsite 3.1
this is my controller :这是我的控制器:
public function import()
{
$data = Excel::toArray(new ProdukImport, request()->file('file'));
if ($data) {
DB::table('produk')
->where('id_produk', $data['id'])
->update($data);
}
}
This is my Import Class:这是我的导入类:
<?php
namespace App\Imports;
use App\Produk;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class ProdukImport implements ToModel, WithHeadingRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Produk([
'id_produk' => $row['id'],
'nama_produk' => $row['produk'],
'harga_jual' => $row['harga']
]);
}
}
this dd($data) result :这个 dd($data) 结果:
array:1 [▼
0 => array:8 [▼
0 => array:3 [▼
"id" => 1.0
"produk" => "Pomade"
"harga" => 90000.0
]
1 => array:3 [▼
"id" => 2.0
"produk" => "Shampoo"
"harga" => 90000.0
]
2 => array:3 [▼
"id" => 3.0
"produk" => "Sikat WC"
"harga" => 90000.0
]
]
]
the $data
result is from this : $data
结果来自:
$data = Excel::toArray(new ProdukImport, request()->file('file'));
Based on the structure of your $data
array, you could probably achieve what you want with something like this:根据$data
数组的结构,您可能可以通过以下方式实现您想要的:
public function import()
{
$data = Excel::toArray(new ProdukImport, request()->file('file'));
return collect(head($data))
->each(function ($row, $key) {
DB::table('produk')
->where('id_produk', $row['id'])
->update(array_except($row, ['id']));
});
}
I had the same issue.我遇到过同样的问题。 Here's how I did it.这是我如何做到的。
My controller looks like this:我的控制器看起来像这样:
public function import(Request $request){
try {
Excel::import(new ProductImport, $request->file('file')->store('temp') );
return redirect()->back()->with('response','Data was imported successfully!');
} catch (\Exception $exception){
return redirect()->back()->withErrors(["msq"=>$exception->getMessage()]);
}
}
And this is the model
method on my ProductImport
class这是我的ProductImport
类的model
方法
public function model(array $row)
{
$product = new Product();
// row[0] is the ID
$product = $product->find($row[0]);
// if product exists and the value also exists
if ($product and $row[3]){
$product->update([
'price'=>$row[3]
]);
return $product;
}
}
how to update data in database with import excel.如何使用导入Excel更新数据库中的数据。 i am using laravel 5.7 and maatwebsite 3.1我正在使用laravel 5.7和maatwebsite 3.1
this is my controller :这是我的控制器:
public function import()
{
$data = Excel::toArray(new ProdukImport, request()->file('file'));
if ($data) {
DB::table('produk')
->where('id_produk', $data['id'])
->update($data);
}
}
This is my Import Class:这是我的导入类:
<?php
namespace App\Imports;
use App\Produk;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class ProdukImport implements ToModel, WithHeadingRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Produk([
'id_produk' => $row['id'],
'nama_produk' => $row['produk'],
'harga_jual' => $row['harga']
]);
}
}
this dd($data) result :此dd($ data)结果:
array:1 [▼
0 => array:8 [▼
0 => array:3 [▼
"id" => 1.0
"produk" => "Pomade"
"harga" => 90000.0
]
1 => array:3 [▼
"id" => 2.0
"produk" => "Shampoo"
"harga" => 90000.0
]
2 => array:3 [▼
"id" => 3.0
"produk" => "Sikat WC"
"harga" => 90000.0
]
]
]
the $data result is from this : $data = Excel::toArray(new ProdukImport, request()->file('file')); $ data的结果是这样的:$ data = Excel :: toArray(new ProdukImport,request()-> file('file'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.