Laravel - 如何导入 excel 文件并使用外键保存到 table

Laravel - How to import excel file file and save into a table with foreign keys

我正在使用 Laravel-5.8 作为 Web 应用程序。另外,我正在使用 maatwebsite-3.1 导入 excel 文件。

型号:HrEmployee

protected $table = 'hr_employees';

protected $fillable = [
          'employee_code',
          'user_id',
          'address',
          'company_id',
          'email',
          'line_manager_id',
          'employee_designation_id',
          'employee_job_title_id',
          'employee_status_id',
          'employee_type_id',
          'employement_type_id',
          'employment_date',
          'first_name',
          'last_name',
      'is_hod'
      ];

public function user()
{
   return $this->belongsTo('App\User');
}

public function parent()
{
   return $this->belongsTo('HrEmployee', 'line_manager_id');
}

public function children()
{
   return $this->hasMany('HrEmployee', 'ine_manager_id');
}

public function company()
{
   return $this->belongsTo('App\Models\Organization\OrgCompany','company_id');
}

public function designation()
{
    return $this->belongsTo('App\Models\Hr\HrDesignation','employee_designation_id');
}

public function jobtitle()
{
    return $this->belongsTo('App\Models\Hr\HrJobTitle','employee_job_title_id');
}

public function employeestatus()
{
    return $this->belongsTo('App\Models\Hr\HrEmployeeStatus','employee_status_id');
}

public function employeetype()
{
    return $this->belongsTo('App\Models\Hr\HrEmployeeType','employee_type_id');
}

public function employementtype()
{
    return $this->belongsTo('App\Models\Hr\HrEmployementType','employement_type_id');
}

public function department()
{
   return $this->belongsTo('App\Models\Organization\OrgDepartment','department_id');
}

我已经配置好 Maatwebsite Excel Package

app/Imports/EmployeesImport.php

namespace App\Imports;

use App\User;
use App\HrEmployee;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class EmployeesImport implements ToModel, WithHeadingRow
{
  public function model(array $row)
  {
    return new HrEmployee([
        'employee_code'     => $row['employee_code'],
        'email'    => $row['email'], 
        'first_name'    => $row['first_name'],
        'last_name'    => $row['last_name'],
        'line_manager_id'    => $row['line_manager_id'],
        'employee_designation_id'    => $row['employee_designation_id'],
        'employee_job_title_id'    => $row['employee_job_title_id'],
    ]);
  }
}

控制器

class HrEmployeesController extends Controller
{
  public function importExportView()
  {
     return view('import');
  }

  public function import() 
  {
    Excel::import(new EmployeesImport,request()->file('file'));

    return back();
  }
}

如何重新编写我的代码以容纳外键 (line_manager_id、employee_designation_id、employee_job_title_id),以便它将键映射到名称。 例如,如果 designation_name 被输入到 excel sheet 它应该将它映射到 employee_designation_id.

谢谢

只需在 EmployeesImport

中找到外键 ID
$row['employee_designation_id'] = HrDesignation::where("name", "like", "%".$row['designation']."%");
$row['line_manager_id']         = HrEmployee::where("first_name", "like", "%".$row['line_manager']."%");
$row['employee_job_title_id']   = HrJobTitle::where("name", "like", "%".$row['job_title']."%");

或完整代码

namespace App\Imports;

use App\User;
use App\HrEmployee;
use App\Hr\HrDesignation;
use App\Hr\HrJobTitle;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class EmployeesImport implements ToModel, WithHeadingRow
{
    public function model(array $row)
    {
        $row['employee_designation_id'] = HrDesignation::where("name", "like", "%".$row['designation']."%");
        $row['line_manager_id']         = HrEmployee::where("first_name", "like", "%".$row['line_manager']."%");
        $row['employee_job_title_id']   = HrJobTitle::where("name", "like", "%".$row['job_title']."%");

        return new HrEmployee([
            'employee_code'           => $row['employee_code'],
            'email'                   => $row['email'],
            'first_name'              => $row['first_name'],
            'last_name'               => $row['last_name'],
            'line_manager_id'         => $row['line_manager_id'],
            'employee_designation_id' => $row['employee_designation_id'],
            'employee_job_title_id'   => $row['employee_job_title_id'],
        ]);
    }
}
namespace App\Imports;

use App\User;
use App\HrEmployee;
use App\Hr\HrDesignation;
use App\Hr\HrJobTitle;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class EmployeesImport implements ToModel, WithHeadingRow
{
    public function model(array $row)
    {
        $employee_designation = HrDesignation::where("name", "like", "%".$row['designation']."%")->first();
        $line_manager = HrEmployee::where("first_name", "like", "%".$row['line_manager']."%")->first();
        $employee_job_title   = HrJobTitle::where("name", "like", "%".$row['job_title']."%")->first();

        $row['employee_designation_id'] = $employee_designation->id;
        $row['line_manager_id'] = $line_manager->id;
        $row['employee_job_title_id'] = $employee_job_title->id;

        return new HrEmployee([
            'employee_code'           => $row['employee_code'],
            'email'                   => $row['email'],
            'first_name'              => $row['first_name'],
            'last_name'               => $row['last_name'],
            'line_manager_id'         => $row['line_manager_id'],
            'employee_designation_id' => $row['employee_designation_id'],
            'employee_job_title_id'   => $row['employee_job_title_id'],
        ]);
    }
}