/*
* @导入excel
*/
public function import(Request $request){
Excel::load($request->file('excel'), function($reader) {
$data = $reader->get();
$temp = $data->toArray();
foreach ($temp[0] as $k => $v) {
Goods::create($v);
}
});
return redirect('admin/commodity')->with([
'message'=>'导入成功!',
'url' =>'/admin/commodity',
]);
}
<form action="{{url('admin/commodity/import')}}" method="post" enctype="multipart/form-data" name="formlist" id="formlist">
{{csrf_field()}}
<div class="add_box add_dao">
<img src="{{asset('images/lodingin_icon.png')}}" alt="">
<a href="javascript:;" class="upload">
<span style="margin-left: 10px; margin-bottom:20px; color: orange">导入</span>
<input type="file" value="" name="excel" class="file_change" onchange="fileupload()" multiple="multiple">
</a>
</div>
</form>
//商品表信息导入成功 function fileupload(){ $("#formlist").submit(); }
.upload{
padding: 5px 10px;
height:13px;
line-height: 13px;
position: relative;
font-size: 18px;
text-decoration: none;
}
.file_change{
position: absolute;
overflow: hidden;
width:85px;
height: 48px;
right: 0;
top: 0;
opacity: 0;
}
/* * @导入商品excel */
public function import(Request $request){
Excel::load($request->file('excel'), function($reader) {
$data = $reader->get();
$temp = $data->toArray();
foreach ($temp[0] as $k => $v) {
Goods::create([ 'class_id'=>$v['商品分类的id'],
'name'=>$v['商品名称'],
'goods_info'=>$v['商品信息'],
'img'=>$v['商品图片'],
'goods_num'=>$v['商品数量'],
'product_code'=>$v['商品编号'],
'site'=>$v['产址'],
'work_company'=>$v['加工企业'],
'productdate'=>$v['保质期'],
n 'area'=>$v['产地'],
'price'=>$v['单价'],
'product_at'=>$v['生产日期'], ]);
} });
return redirect('admin/commodity');
// ->with([ // 'message'=>'导入成功!', // 'url' =>'/admin/commodity', // ]); }
要注意主键冲突,主键一般是自增的
注意: config/excel.php
// 改为false,这下就算是中文标题也OK
'to_ascii' => false,
'heading' => 'slugged',//中文
// 指的是表格头部,laravel excel一般不读
// 'heading' => 'original',//英文