gpt4 book ai didi

excel - Laravel Excel 多表 primary_id 作为 foreign_id 保存到另一个表

转载 作者:行者123 更新时间:2023-12-04 20:26:58 25 4
gpt4 key购买 nike

我可以在 Laravel 中成功导入 Excel 数据,并将多个表插入到数据库中。我正在使用 maatwebsite/excel 版本 3 composer 包和 laravel 版本 5.8

如何在成员表主 ID 的 licence_application 表(外部 ID)中插入 mamber_id 值。

导入类文件:

class UsersImport implements ToCollection
{
/**
* @param Collection $collection
*/
public function collection(Collection $rows)
{

foreach ($rows as $row){
Member::create([
'full_name' => $row[0],
'father_name' => $row[1],
'mother_name' => $row[2],
'cell_number' => $row[3],
]);
LicenceApplication::create([
'member_id' => ???
'licence_category_name' => $row[4],
]);
LicenceSuccess::create([
'application_type' => $row[5],
]);
}

}
}

Controller 文件:ImporExcelController.php

public function import(Request $request)
{
$this->validate($request, [
'select_file' => 'required|mimes:xls,xlsx'
]);

$path = $request->file('select_file')->getRealPath();

$data = Excel::import(new UsersImport, $path);

return back()->with('success', 'Import data successfully!');
}

Excel 文件截图:https://imgur.com/a/XhxBRpW

请问如何解决这个问题?

最佳答案

我用代码解决了这个问题。

foreach ($rows as $row){
$memberId = Member::create([
'full_name' => $row['full_name'],
'father_name' => $row['father_name'],
'mother_name' => $row['mother_name'],
'cell_number' => $row['cell_number'],
]);
$applicationId = LicenceApplication::create([
'member_id' => $memberId->member_id,
'licence_category_name' => $row['licence_category_name'],
]);
LicenceSuccess::create([
'member_id' => $memberId->member_id,
'application_id' => $applicationId->application_id,
'application_type' => $row['application_type'],
]);
}

关于excel - Laravel Excel 多表 primary_id 作为 foreign_id 保存到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58320277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com