laravel Excel 匯入
阿新 • • 發佈:2020-06-24
<?php
namespace App\Modules\Live\Http\Controllers; use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
class ImportController extends Controller
{
public function import(){
try { $request = request();
$file = $request->file("file");$originalName = $file->getClientOriginalName(); // 檔案原名
$realPath = $file->getRealPath();
Excel::load($realPath, function($reader) use($file){
$data = $reader->getSheet(0)->toArray();
foreach($data as $key => $value){if($key !=0){
$tmp['realname']=$value[0];
$tmp['mobile']=$value[1];
$tmp['idcard']=$value[2];
$tmp['passport']=$value[3];
$tmp['company_name']=$value[4];
$tmp['uniacid']=4;$tmp['created_at']=time();
User::create($tmp);
}
}
});
$this->_addLog('匯入資料');
return $this->result(0,'匯入成功',['redirect' => wgRoute('member.index')]);
}catch(Exception $e){
return $this->error($e);
}
}
}
?>
如果需要在匯入Excel資料中傳遞其他引數,可通過use()方法,以陣列的形式進行傳遞
Excel::load($realPath, function($reader) use($options){}