将 Faker 数据添加到 Laravel 数据库
Adding Faker data to Laravel Database
我正在 Laravel 创建此应用程序。
我有 3 tables,员工,部门和薪水
Employees table 包含列 id、name、dept_name、gender 等。
部门有 dept_name、emp_id
Salaries 有 monthlySalaries, emp_id, dept_name
以上所有table都是由这些各自的模型创建的。
我面临的问题是,当我 运行 DatabaseSeeder 时,值确实输入到数据库中,但它们不一致,例如。员工 table with id = 1 and Dept_name = Production, 而部门 table emp_id = 1 and Dept_name = Tech Support.
这个问题也影响到工资table。
我知道,我做错了什么,但我似乎无法弄清楚是什么。
我尝试在引用 emp_id(salaries) 的 id(employee table) 上创建 PK,但没有成功。
我什至尝试更新下面的 DB seeders 脚本,但这不会创建工资条目,在某些情况下甚至将 emp_id(departments) 留空。
对我做错的任何 help/pointers 将不胜感激。
感谢您抽出时间阅读本文:)
员工模型:
Schema::create('employees', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('dept_name');
$table->string('name');
$table->string('gender');
$table->timestamp('date_of_joining');
$table->dateTime('date_of_birth');
$table->timestamps();
}
部门模型:
Schema::create('departments', function (Blueprint $table) {
$table->string('dept_name');
$table->unsignedInteger('employee_id');
$table->timestamps();
});
薪酬模式:
Schema::create('salaries', function (Blueprint $table) {
$table->string('monthlySalary');
$table->unsignedInteger('employee_id');
$table->string('dept_name');
$table->timestamps();
});
数据库播种机
public function run()
{
$this->call([UsersTableSeeder::class]);
//creates employees & Departments
factory(App\Employee::class, 25)->create()->each(function ($employee) {
$department = factory(App\Department::class)->make();
$employee->department()->save($department);
$salary = factory(App\Salary::class)->make();
$employee->salary()->save($salary);
});
}
App\Employee
public function department()
{
return $this->hasOne(Department::class);
}
public function Salary()
{
return $this->hasOne(Salary::class);
}
App\Department
public function employees()
{
return $this->hasMany(Employee::class);
}
public function salaries()
{
return $this->hasMany(Salary::class);
}
App\Salary
public function employeeSalary()
{
return $this->belongsTo(Employee::class);
}
您编写的 Laravel 代码看起来是正确的,并且看起来它正在做它应该做的事情 - 我认为这个问题主要是由于您的数据库方案。
理想情况下,您应该退后一步,考虑您的数据库方案如何为您正在使用的对象建模。比如你现在的scheme中可能有如下关系:
- 一个
Employee
有零个或多个Departments
- 一个
Employee
有零个或多个Salaries
并且一些数据在 table 中是重复的(我认为 1):
- 一个
Employee
有一个dept_name
并且还与零个或多个Departments
相关联,每个dept_name
我认为你真正想要的是:
- 一个
Employee
正好属于一个Department
- 一个
Employee
正好有一个Salary
可以通过像这样构建 table 来避免此类问题:
employees departments salaries
--------- ----------- --------
id id id
department_id name monthly_salary
salary_id
name
gender
...other columns...
每个员工只有一个department_id
,它指的是departments
table中的一行(员工和部门之间的一对多关系)。
每个员工只有一个 salary_id
,它引用 salaries
table 中的一行(员工与薪水之间的一对多关系)。
如果您以这种方式构建数据,员工的部门名称来自 departments
table(仅 来自 table ).不会有部门名称不匹配的问题,因为该信息只有一个来源。
关系数据库中这种数据建模方法的术语是数据库规范化。网络上有很多资源可以指导您更好地构建数据。
1 我假设一名员工只能属于一个部门。这可能不是您要建模的内容。
我正在 Laravel 创建此应用程序。 我有 3 tables,员工,部门和薪水 Employees table 包含列 id、name、dept_name、gender 等。 部门有 dept_name、emp_id Salaries 有 monthlySalaries, emp_id, dept_name
以上所有table都是由这些各自的模型创建的。
我面临的问题是,当我 运行 DatabaseSeeder 时,值确实输入到数据库中,但它们不一致,例如。员工 table with id = 1 and Dept_name = Production, 而部门 table emp_id = 1 and Dept_name = Tech Support. 这个问题也影响到工资table。
我知道,我做错了什么,但我似乎无法弄清楚是什么。 我尝试在引用 emp_id(salaries) 的 id(employee table) 上创建 PK,但没有成功。 我什至尝试更新下面的 DB seeders 脚本,但这不会创建工资条目,在某些情况下甚至将 emp_id(departments) 留空。
对我做错的任何 help/pointers 将不胜感激。 感谢您抽出时间阅读本文:)
员工模型:
Schema::create('employees', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('dept_name');
$table->string('name');
$table->string('gender');
$table->timestamp('date_of_joining');
$table->dateTime('date_of_birth');
$table->timestamps();
}
部门模型:
Schema::create('departments', function (Blueprint $table) {
$table->string('dept_name');
$table->unsignedInteger('employee_id');
$table->timestamps();
});
薪酬模式:
Schema::create('salaries', function (Blueprint $table) {
$table->string('monthlySalary');
$table->unsignedInteger('employee_id');
$table->string('dept_name');
$table->timestamps();
});
数据库播种机
public function run()
{
$this->call([UsersTableSeeder::class]);
//creates employees & Departments
factory(App\Employee::class, 25)->create()->each(function ($employee) {
$department = factory(App\Department::class)->make();
$employee->department()->save($department);
$salary = factory(App\Salary::class)->make();
$employee->salary()->save($salary);
});
}
App\Employee
public function department()
{
return $this->hasOne(Department::class);
}
public function Salary()
{
return $this->hasOne(Salary::class);
}
App\Department
public function employees()
{
return $this->hasMany(Employee::class);
}
public function salaries()
{
return $this->hasMany(Salary::class);
}
App\Salary
public function employeeSalary()
{
return $this->belongsTo(Employee::class);
}
您编写的 Laravel 代码看起来是正确的,并且看起来它正在做它应该做的事情 - 我认为这个问题主要是由于您的数据库方案。
理想情况下,您应该退后一步,考虑您的数据库方案如何为您正在使用的对象建模。比如你现在的scheme中可能有如下关系:
- 一个
Employee
有零个或多个Departments
- 一个
Employee
有零个或多个Salaries
并且一些数据在 table 中是重复的(我认为 1):
- 一个
Employee
有一个dept_name
并且还与零个或多个Departments
相关联,每个dept_name
我认为你真正想要的是:
- 一个
Employee
正好属于一个Department
- 一个
Employee
正好有一个Salary
可以通过像这样构建 table 来避免此类问题:
employees departments salaries
--------- ----------- --------
id id id
department_id name monthly_salary
salary_id
name
gender
...other columns...
每个员工只有一个department_id
,它指的是departments
table中的一行(员工和部门之间的一对多关系)。
每个员工只有一个 salary_id
,它引用 salaries
table 中的一行(员工与薪水之间的一对多关系)。
如果您以这种方式构建数据,员工的部门名称来自 departments
table(仅 来自 table ).不会有部门名称不匹配的问题,因为该信息只有一个来源。
关系数据库中这种数据建模方法的术语是数据库规范化。网络上有很多资源可以指导您更好地构建数据。
1 我假设一名员工只能属于一个部门。这可能不是您要建模的内容。