如何解决Laravel - 如何使用 Laravel Maatwebsites 导入两 (2) 个表
在我的 Laravel-5.8 和 Maatwebsites-3.1 中,我有这两个模型:
HrEmployee (hr_employees): id,first_name,last_name,email hrDepartment(hr_departments):id部门名称
我只有一张 Excel 表格:
名字 |姓氏 |官方邮箱 |部门
class FirstEmployeeSheetImport implements ToModel,WithHeadingRow,WithBatchInserts,WithValidation
{
protected $department;
private $errors = []; // array to accumulate errors
use Importable;
public function model(array $row)
{
$this->department = $row['department'];
$employee_data = [
'first_name' => $row['first_name'],'last_name' => $row['last_name'],'email' => $row['official_email'],'department_id' => $this->getDepartment(),];
$employee = HrEmployee::create($employee_data);
}
public function getDepartment(){
if(!empty($this->department) || !$this->department){
return HrDepartment::where('dept_name',$this->department)->where('company_id',Auth::user()->company_id)->pluck('id')->first();
} else {
return 0;
}
}
}
当提交导入按钮时,我希望应用程序首先检查 Excel 表中的部门列,然后插入到 hr_departments 表中的部门名称中,然后再将其他详细信息导入到 hr_employees (First Name | Last Name |官方邮箱|部门)
我如何实现这一目标?
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。