我尝试将excel文件导入到一个表中,但效果很好,但问题是Census模块,它允许用户添加新的居民以及所需的详细信息,现在每次用户添加一个新居民还通过使用“居民”表中的user_no外键向用户表反映。我想在导入Excel文件时实现这一目标。那可能吗? (对不起,我还是laravel的新手)
这是我添加居民的代码:
public function store(Request $request)
{
$residents = new Resident;
$residents->resident_fname = $request->input('resident_fname');
$residents->resident_lname = $request->input('resident_lname');
$residents->resident_mi = $request->input('resident_mi');
$residents->resident_email = $request->input('resident_email');
$residents->resident_age = $request->input('resident_age');
$residents->resident_dob = $request->input('resident_dob');
$residents->role = 'resident';
$residents->resident_address = $request->input('resident_address');
$residents->resident_contact = $request->input('resident_contact');
$residents->resident_gender = $request->input('resident_gender');
$residents->ResidentVoter_status = $request->input('ResidentVoter_status');
$residents->resident_status = $request->input('resident_status');
$residents->resident_religion = $request->input('resident_religion');
$residents->resident_purok = $request->input('resident_purok');
$residents->save();
$res_id = Resident::orderBy('created_at', 'desc')->first();
$user = new User;
$user->name = $request->input('resident_fname');
$user->email = $request->input('resident_email');
$user->password = Hash::make('123456');
$user->role = 'resident';
$user->res_id = $res_id->id;
$user->save();
return redirect('/residents')->with('success', 'Successfully Added!');
}
这是我的ResidentImport模型的代码:
public function model(array $row)
{
return new Resident([
'resident_fname' => $row[0],
'resident_lname' => $row[1],
'resident_mi' => $row[2],
'resident_dob' => \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['3']),
'role' => $row[4],
'resident_age' => $row[5],
'resident_address' => $row[6],
'resident_contact' => $row[7],
'resident_email' => $row[8],
'resident_purok' => $row[9],
'resident_status' => $row[10],
'resident_gender' => $row[11],
'resident_religion' => $row[12],
'ResidentVoter_status' => $row[13],
]);
}
这是我的ResidentImport控制器:
public function import(Request $request)
{
$import = Excel::import(new ResidentImport, request()->file('import_file'));
return redirect('/importing')->with('success', 'Imported Successfully!');
}
我绝对不知道该怎么办。有人可以帮我吗?