Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数据库Seeder和Laravel的问题_Laravel_Faker - Fatal编程技术网

数据库Seeder和Laravel的问题

数据库Seeder和Laravel的问题,laravel,faker,Laravel,Faker,我有3个表,模式如下。 员工, EmployeeDB.php $factory->define(Employee::class, function (Faker $faker) { return [ 'emp_id' => $faker->randomDigit, 'name' => $faker->name, 'dept_id' => $faker->numberBetween($min = 1,

我有3个表,模式如下。 员工,

EmployeeDB.php

$factory->define(Employee::class, function (Faker $faker) {
    return [
        'emp_id' => $faker->randomDigit,
        'name' => $faker->name,
        'dept_id' => $faker->numberBetween($min = 1, $max = 15),
        'salary_id' => $faker->randomDigit(),
        'gender' => $faker->randomElement(['M', 'F', 'T']),
       /*if I remove these next 2 statements, I receive an error for 
       SQLSTATE[42S22]: Column not found: 1054 Unknown column 
       'employee_id' in 'field list' (SQL: insert into `employees` (`emp_id`, `name`, `dept_id`, `salary_id`, `gender`, `date_of_joining`,
        `date_of_birth`, `employee_id`, `updated_at`, `created_at`) values (2, Jamaal Beer, 3, 9, T, 2018-05-05 18:59:40, 2005-07-05 13:17:23, ?, 
        2019-12-11 11:15:42, 2019-12-11 11:15:42))
       */ 
       'employee_id' => $faker->randomDigit,
       //Same for this one as well
        'department_id' => $faker->randomDigit,
        'date_of_joining' => $faker->dateTimeBetween($startDate = '-5 years', $endDate = 'now', $timezone = null),
        'date_of_birth' => $faker->dateTimeBetween($startDate = '-20 years', $endDate = 'now', $timezone = null),
factory->define(Department::class, function (Faker $faker) {
return [
    //Yes, this is a hack. Use Multidimensional Arrays the next time.
    'id' => $faker->numberBetween($min = 1, $max = 15),
    'dept_name' => $faker->randomElement(['Production', 'Purchase & Quality', 'Operations', 'Sales', 'Customer Serice', 'Business Development', 'Maketing', 'Tech Support', 'Finance', 'Human Resources', 'Research & Development', 'IT', 'Legal']),
];
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
 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);
    });
 public function run()
    {
        $this->call([UsersTableSeeder::class]);//this is not important
        factory('App\Department', 12)->create();
        factory('App\Salary', 50)->create();
        factory('App\Employee', 55)->create();
    }
}
员工迁移表:

Schema::create('employees', function (Blueprint $table) {
        $table->bigIncrements('emp_id');
        $table->integer('dept_id')->unsigned();
        $table->foreign('dept_id')->references('id')->on('departments');
        $table->integer('salary_id')->unsigned();
        $table->foreign('salary_id')->references('id')->on('salaries');
        $table->string('name');
        $table->string('gender');
        $table->timestamp('date_of_joining');
        $table->dateTime('date_of_birth');
        /*SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'employee_id' cannot be null 
        (SQL: insert into `employees` (`emp_id`, `name`, `dept_id`, `salary_id`, `gender`, `employee_id`, `department_id`, 
        `date_of_joining`, `date_of_birth`, `updated_at`, `created_at`) 
        values (6, Martina Wuckert, 7, 3, F, ?, 3, 2018-09-14 05:59:15, 20
        */
        $table->string('employee_id')->nullable();
        $table->string('department_id')->nullable();
        $table->timestamps();
        //added to prevent errors
部门 DepartmentsDB.php

$factory->define(Employee::class, function (Faker $faker) {
    return [
        'emp_id' => $faker->randomDigit,
        'name' => $faker->name,
        'dept_id' => $faker->numberBetween($min = 1, $max = 15),
        'salary_id' => $faker->randomDigit(),
        'gender' => $faker->randomElement(['M', 'F', 'T']),
       /*if I remove these next 2 statements, I receive an error for 
       SQLSTATE[42S22]: Column not found: 1054 Unknown column 
       'employee_id' in 'field list' (SQL: insert into `employees` (`emp_id`, `name`, `dept_id`, `salary_id`, `gender`, `date_of_joining`,
        `date_of_birth`, `employee_id`, `updated_at`, `created_at`) values (2, Jamaal Beer, 3, 9, T, 2018-05-05 18:59:40, 2005-07-05 13:17:23, ?, 
        2019-12-11 11:15:42, 2019-12-11 11:15:42))
       */ 
       'employee_id' => $faker->randomDigit,
       //Same for this one as well
        'department_id' => $faker->randomDigit,
        'date_of_joining' => $faker->dateTimeBetween($startDate = '-5 years', $endDate = 'now', $timezone = null),
        'date_of_birth' => $faker->dateTimeBetween($startDate = '-20 years', $endDate = 'now', $timezone = null),
factory->define(Department::class, function (Faker $faker) {
return [
    //Yes, this is a hack. Use Multidimensional Arrays the next time.
    'id' => $faker->numberBetween($min = 1, $max = 15),
    'dept_name' => $faker->randomElement(['Production', 'Purchase & Quality', 'Operations', 'Sales', 'Customer Serice', 'Business Development', 'Maketing', 'Tech Support', 'Finance', 'Human Resources', 'Research & Development', 'IT', 'Legal']),
];
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
 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);
    });
 public function run()
    {
        $this->call([UsersTableSeeder::class]);//this is not important
        factory('App\Department', 12)->create();
        factory('App\Salary', 50)->create();
        factory('App\Employee', 55)->create();
    }
}
部门迁移

Schema::create('departments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('dept_name');
        $table->timestamps();
    });
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->unsignedInteger('emp_id', true);
            $table->unsignedInteger('dept_id');
            $table->foreign('dept_id')->references('id')->on('departments');
            $table->unsignedInteger('salary_id');
            $table->foreign('salary_id')->references('id')->on('salaries');
            $table->string('name');
            $table->string('gender');
            $table->timestamp('date_of_joining');
            $table->dateTime('date_of_birth');
            $table->timestamps();
            //added to prevent errors
        });
 protected $dept_id = 'id';
public function up()
    {

        Schema::create('departments', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('dept_name');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('salaries', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('monthlySalary');
            $table->timestamps();
        });
    }
薪水

SalaryDb.php

$factory->define(Employee::class, function (Faker $faker) {
    return [
        'emp_id' => $faker->randomDigit,
        'name' => $faker->name,
        'dept_id' => $faker->numberBetween($min = 1, $max = 15),
        'salary_id' => $faker->randomDigit(),
        'gender' => $faker->randomElement(['M', 'F', 'T']),
       /*if I remove these next 2 statements, I receive an error for 
       SQLSTATE[42S22]: Column not found: 1054 Unknown column 
       'employee_id' in 'field list' (SQL: insert into `employees` (`emp_id`, `name`, `dept_id`, `salary_id`, `gender`, `date_of_joining`,
        `date_of_birth`, `employee_id`, `updated_at`, `created_at`) values (2, Jamaal Beer, 3, 9, T, 2018-05-05 18:59:40, 2005-07-05 13:17:23, ?, 
        2019-12-11 11:15:42, 2019-12-11 11:15:42))
       */ 
       'employee_id' => $faker->randomDigit,
       //Same for this one as well
        'department_id' => $faker->randomDigit,
        'date_of_joining' => $faker->dateTimeBetween($startDate = '-5 years', $endDate = 'now', $timezone = null),
        'date_of_birth' => $faker->dateTimeBetween($startDate = '-20 years', $endDate = 'now', $timezone = null),
factory->define(Department::class, function (Faker $faker) {
return [
    //Yes, this is a hack. Use Multidimensional Arrays the next time.
    'id' => $faker->numberBetween($min = 1, $max = 15),
    'dept_name' => $faker->randomElement(['Production', 'Purchase & Quality', 'Operations', 'Sales', 'Customer Serice', 'Business Development', 'Maketing', 'Tech Support', 'Finance', 'Human Resources', 'Research & Development', 'IT', 'Legal']),
];
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
 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);
    });
 public function run()
    {
        $this->call([UsersTableSeeder::class]);//this is not important
        factory('App\Department', 12)->create();
        factory('App\Salary', 50)->create();
        factory('App\Employee', 55)->create();
    }
}
工资分配

Schema::create('departments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('dept_name');
        $table->timestamps();
    });
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->unsignedInteger('emp_id', true);
            $table->unsignedInteger('dept_id');
            $table->foreign('dept_id')->references('id')->on('departments');
            $table->unsignedInteger('salary_id');
            $table->foreign('salary_id')->references('id')->on('salaries');
            $table->string('name');
            $table->string('gender');
            $table->timestamp('date_of_joining');
            $table->dateTime('date_of_birth');
            $table->timestamps();
            //added to prevent errors
        });
 protected $dept_id = 'id';
public function up()
    {

        Schema::create('departments', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('dept_name');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('salaries', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('monthlySalary');
            $table->timestamps();
        });
    }
App\Department 班级部门扩展模式

{  
    //
    public function employee()
    {
        return $this->hasMany(Employee::class);
    }
    public function salary()
    {
        return $this->hasMany(Salary::class);
    }
}
App\Employee

class Employee extends Model
    {  //belongsto  block
        public function department()
        {
            return $this->hasOne(Department::class);
        }
        public function Salary()
        {
            return $this->hasOne(Salary::class);
        }
    }
App\Salary

    class Salary extends Model
{
    //
    public function employee()
    {
        return $this->belongsTo(Employee::class);
    }
}
DatabaseSeeder.php

$factory->define(Employee::class, function (Faker $faker) {
    return [
        'emp_id' => $faker->randomDigit,
        'name' => $faker->name,
        'dept_id' => $faker->numberBetween($min = 1, $max = 15),
        'salary_id' => $faker->randomDigit(),
        'gender' => $faker->randomElement(['M', 'F', 'T']),
       /*if I remove these next 2 statements, I receive an error for 
       SQLSTATE[42S22]: Column not found: 1054 Unknown column 
       'employee_id' in 'field list' (SQL: insert into `employees` (`emp_id`, `name`, `dept_id`, `salary_id`, `gender`, `date_of_joining`,
        `date_of_birth`, `employee_id`, `updated_at`, `created_at`) values (2, Jamaal Beer, 3, 9, T, 2018-05-05 18:59:40, 2005-07-05 13:17:23, ?, 
        2019-12-11 11:15:42, 2019-12-11 11:15:42))
       */ 
       'employee_id' => $faker->randomDigit,
       //Same for this one as well
        'department_id' => $faker->randomDigit,
        'date_of_joining' => $faker->dateTimeBetween($startDate = '-5 years', $endDate = 'now', $timezone = null),
        'date_of_birth' => $faker->dateTimeBetween($startDate = '-20 years', $endDate = 'now', $timezone = null),
factory->define(Department::class, function (Faker $faker) {
return [
    //Yes, this is a hack. Use Multidimensional Arrays the next time.
    'id' => $faker->numberBetween($min = 1, $max = 15),
    'dept_name' => $faker->randomElement(['Production', 'Purchase & Quality', 'Operations', 'Sales', 'Customer Serice', 'Business Development', 'Maketing', 'Tech Support', 'Finance', 'Human Resources', 'Research & Development', 'IT', 'Legal']),
];
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
 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);
    });
 public function run()
    {
        $this->call([UsersTableSeeder::class]);//this is not important
        factory('App\Department', 12)->create();
        factory('App\Salary', 50)->create();
        factory('App\Employee', 55)->create();
    }
}
当我运行下面的命令时,这是我得到的错误 php artisan数据库:种子

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`volga_2`.`employees`, CONSTRAINT `employees_dept_id_foreign` FOREIGN KEY (`dept_id`) REFERENCES `departments` (`id`))")
我想做的是:为每个员工记录分配一个部门和一份工资

这是供以后使用的:部门不能在其自己的表中复制。即,一旦创建了名为的部门id,就无法再次创建

我知道我做错了,否则就不会有错误。当我为部门和薪水运行dbSeeder时,它工作得很好。但是,每当有重复的部门条目时,它就会抛出一个错误。但是存储在数据库中的数据很好

我想要一个解决方案或一些建议,因为我还在学习拉威尔

谢谢

编辑 当我执行 php artisan迁移

迁移顺序

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.44 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (1.18 seconds)
Migrating: 2019_12_09_105432_create_departments_table
Migrated:  2019_12_09_105432_create_departments_table (0.29 seconds)
Migrating: 2019_12_09_105743_create_salaries_table
Migrated:  2019_12_09_105743_create_salaries_table (0.21 seconds)
Migrating: 2019_12_10_104739_create_employees_table
Migrated:  2019_12_10_104739_create_employees_table (2.04 seconds)

只需更改迁移的顺序。我猜您是在创建表之前尝试建立关系。对父表进行第一次迁移

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`volga_2`.`employees`, CONSTRAINT `employees_dept_id_foreign` FOREIGN KEY (`dept_id`) REFERENCES `departments` (`id`))")
此错误表示由于某种原因无法生成与部门表的关系。在编写过程中,您尝试在生成其他两个表之前,根据它们的关系生成
Employee
,通过更改迁移的名称,您可以重新排序迁移。这意味着您可以在迁移名称中随时间的变化更改运行迁移的顺序,以便按照以下顺序进行迁移:

1) 部门迁移

2) 工薪


3) 员工迁移

感谢大家提供解决方案和想法,帮助我解决这个问题。但在修补之后,我找到了一个很好的解决方案。 我将只是张贴我所做的更改,以使它更容易保持跟踪,它可能会对其他人有所帮助

给你

EmployeeDB.php

$factory->define(Employee::class, function (Faker $faker) {
    return [
        'emp_id' => $faker->unique()->randomNumber($nbDigits = 3, $strict = true),
        'name' => $faker->name,
        //dept_id and salary_id pulls the data from their respective tables and then //randomly assigns an id from the id column
        'dept_id' => App\Department::all()->random()->id,
        'salary_id' => App\Salary::all()->random()->id,
        'gender' => $faker->randomElement(['M', 'F', 'T']),
        'date_of_joining' => $faker->dateTimeBetween($startDate = '-5 years', $endDate = 'now', $timezone = null),
        'date_of_birth' => $faker->dateTimeBetween($startDate = '-20 years', $endDate = 'now', $timezone = null),

    ];
});
员工迁移

Schema::create('departments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('dept_name');
        $table->timestamps();
    });
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->unsignedInteger('emp_id', true);
            $table->unsignedInteger('dept_id');
            $table->foreign('dept_id')->references('id')->on('departments');
            $table->unsignedInteger('salary_id');
            $table->foreign('salary_id')->references('id')->on('salaries');
            $table->string('name');
            $table->string('gender');
            $table->timestamp('date_of_joining');
            $table->dateTime('date_of_birth');
            $table->timestamps();
            //added to prevent errors
        });
 protected $dept_id = 'id';
public function up()
    {

        Schema::create('departments', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('dept_name');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('salaries', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('monthlySalary');
            $table->timestamps();
        });
    }
部门数据库

$factory->define(Department::class, function (Faker $faker) {
    return [
        //Yes, this is a hack. Use Multidimensional Arrays the next time.
        'id' => $faker->unique()->numberBetween(1, 12),
        'dept_name' => $faker->randomElement(['Production', 'Purchase & Quality', 'Operations', 'Sales', 'Customer Serice', 'Business Development', 'Maketing', 'Tech Support', 'Finance', 'Human Resources', 'Research & Development', 'IT', 'Legal']),
    ];
});
部门迁移

Schema::create('departments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('dept_name');
        $table->timestamps();
    });
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->unsignedInteger('emp_id', true);
            $table->unsignedInteger('dept_id');
            $table->foreign('dept_id')->references('id')->on('departments');
            $table->unsignedInteger('salary_id');
            $table->foreign('salary_id')->references('id')->on('salaries');
            $table->string('name');
            $table->string('gender');
            $table->timestamp('date_of_joining');
            $table->dateTime('date_of_birth');
            $table->timestamps();
            //added to prevent errors
        });
 protected $dept_id = 'id';
public function up()
    {

        Schema::create('departments', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('dept_name');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('salaries', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('monthlySalary');
            $table->timestamps();
        });
    }
薪水数据库

$factory->define(Salary::class, function (Faker $faker) {
        return [
            'id' => $faker->unique()->randomNumber($nbDigits = 5, $strict = true),
            'monthlySalary' => $faker->randomNumber($nbDigits = 3),
            //Yes this is a hack. Use MultiDimensional Arrays the next time.
        ];
    });
薪资迁移

Schema::create('departments', function (Blueprint $table) {
        $table->increments('id');
        $table->string('dept_name');
        $table->timestamps();
    });
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->unsignedInteger('emp_id', true);
            $table->unsignedInteger('dept_id');
            $table->foreign('dept_id')->references('id')->on('departments');
            $table->unsignedInteger('salary_id');
            $table->foreign('salary_id')->references('id')->on('salaries');
            $table->string('name');
            $table->string('gender');
            $table->timestamp('date_of_joining');
            $table->dateTime('date_of_birth');
            $table->timestamps();
            //added to prevent errors
        });
 protected $dept_id = 'id';
public function up()
    {

        Schema::create('departments', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('dept_name');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('salaries', function (Blueprint $table) {
            $table->unsignedInteger('id', true);
            $table->string('monthlySalary');
            $table->timestamps();
        });
    }
DatabaseSeeder.php

$factory->define(Employee::class, function (Faker $faker) {
    return [
        'emp_id' => $faker->randomDigit,
        'name' => $faker->name,
        'dept_id' => $faker->numberBetween($min = 1, $max = 15),
        'salary_id' => $faker->randomDigit(),
        'gender' => $faker->randomElement(['M', 'F', 'T']),
       /*if I remove these next 2 statements, I receive an error for 
       SQLSTATE[42S22]: Column not found: 1054 Unknown column 
       'employee_id' in 'field list' (SQL: insert into `employees` (`emp_id`, `name`, `dept_id`, `salary_id`, `gender`, `date_of_joining`,
        `date_of_birth`, `employee_id`, `updated_at`, `created_at`) values (2, Jamaal Beer, 3, 9, T, 2018-05-05 18:59:40, 2005-07-05 13:17:23, ?, 
        2019-12-11 11:15:42, 2019-12-11 11:15:42))
       */ 
       'employee_id' => $faker->randomDigit,
       //Same for this one as well
        'department_id' => $faker->randomDigit,
        'date_of_joining' => $faker->dateTimeBetween($startDate = '-5 years', $endDate = 'now', $timezone = null),
        'date_of_birth' => $faker->dateTimeBetween($startDate = '-20 years', $endDate = 'now', $timezone = null),
factory->define(Department::class, function (Faker $faker) {
return [
    //Yes, this is a hack. Use Multidimensional Arrays the next time.
    'id' => $faker->numberBetween($min = 1, $max = 15),
    'dept_name' => $faker->randomElement(['Production', 'Purchase & Quality', 'Operations', 'Sales', 'Customer Serice', 'Business Development', 'Maketing', 'Tech Support', 'Finance', 'Human Resources', 'Research & Development', 'IT', 'Legal']),
];
$factory->define(Salary::class, function (Faker $faker) {
    return [
        'id' => $faker->randomDigit(),
        'monthlySalary' => $faker->randomNumber($nbDigits = 3),
        //Yes this is a hack. Use MultiDimensional Arrays the next time.
    ];
});
 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);
    });
 public function run()
    {
        $this->call([UsersTableSeeder::class]);//this is not important
        factory('App\Department', 12)->create();
        factory('App\Salary', 50)->create();
        factory('App\Employee', 55)->create();
    }
}
发生的事情是,我首先在Departments表中添加了Departments ID及其各自的名称。然后,我播种了工资表,只包含工资id和工资金额。 然后运行employees seeder,它通过外键关系引用departments和salary表,并将这些表中id列的数据插入my employees表中的dept\u id和salary\u id列

设置主键的另一个重要方面是使用

protected $id = 'Primary Key Table name'
请参阅上面的DepartmentMigration代码块。这需要在up()函数的作用域之外定义,并且必须存在于需要定义主键的任何位置

我已经做了3个多小时了,每一个DB播种机都是这样的,所以我很高兴这能奏效。可能有更好更优雅的解决方案,但我明天会继续讨论。:)

感谢您的阅读,我希望这对您有所帮助。
我将尽我所能回答任何与此相关的问题。:)

嗨!,迁移的顺序是部门表、工资表,然后是员工表。因为部门和薪资表都有主键,这些主键由employees表引用。所以我需要在employees表存在之前先创建这些表,否则也会出现错误。您建议的迁移顺序更改也是我目前正在做的。我已将其添加到问题中。这个错误就是其中之一。当我运行播种机时,首先创建的是员工,而不是部门或工资。这对员工来说是失败的。员工与部门之间存在“一元”关系,而部门之间存在“多元”关系。工资也有同样的关系