![](/img/trans.png)
[英]In laravel, how to import bulk large data “quickly” from excel sheet?
[英]Is it possible to import data to multiple tables from one excel sheet using laravel excel?
我們正在創建一個用戶可以上載包含多個表信息的excel文件的應用程序。 (例如,請參見下面的屏幕截圖)。 是否可以拆分這些數據並創建多個模型以使用laravel excel導入數據庫? 如果沒有,您是否建議使用另一個軟件包?
如果我錯了,請糾正我,但據我了解,您每次在控制器中只能導入一個模型。
控制器:
class ImportsController
{
public function import()
{
Excel::import(new DataImport, 'testSheet.xlsx'); //only allows for one model
return redirect('/')->with('success', 'All good!');
}
}
進口:
class DataImport implements ToModel
{
public function model(array $row)
{
// need way to create and return multiple models
return new Data([
]);
}
}
路線:
Route::get('/import', 'ImportsController@import');
第一行是表名,第二行是字段名,接下來的所有行都是數據
除了導入模型,還可以導入集合,並在每行上創建兩個模型:
namespace App\Imports;
use App\EmployeeType;
use App\Contact;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class EmployeesImport implements ToCollection
{
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
EmployeeType::create([
'name' => $row[1],
]);
Contact::create([
'institution' => $row[3],
'type' => $row[4],
'address' => $row[5],
]);
}
}
}
在這里閱讀更多
可以使用WithMultipleSheets關注一次導入多個工作表,同時進行多個導入,但是據我所知,您不能使用此功能從單個工作表創建多個導入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.