【发布时间】:2018-05-17 05:27:04
【问题描述】:
我有三个模型
Developer 模型:
迁移:
$table->increments('id');
$table->string('email')->unique();
$table->unsignedInteger('programming_language_id');
$table->unsignedInteger('language_id');
$table->timestamps();
和功能
class Developer extends Model
{
public function programming_languages() {
return $this->hasMany('App\ProgrammingLanguage');
}
public function languages() {
return $this->hasMany('App\Language');
}
}
ProgrammingLanguage 模型:
迁移:
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
和功能:
protected $table = 'programming_languages';
public function developers() {
return $this->belongsToMany('App\Developer');
}
语言模型:
迁移:
$table->increments('id');
$table->string('code', 30)->unique();
$table->timestamps();
和功能:
public function developers() {
return $this->belongsToMany('App\Developer');
}
我想制作 db 播种机并成为它们之间的关系。我怎样才能做到这一点? 我正在尝试:创建工厂 DeveloperFactory
$factory->define(App\Developer::class, function (Faker $faker) {
return [
'email' => $faker->unique()->safeEmail,
'programming_language_id' => function () {
return factory(App\ProgrammingLanguage::class)->create()->id;
},
'language_id' => function () {
return factory(App\Language::class)->create()->id;
}
];
});
seed 可以,但它不会建立任何关系。只需播种数据。
我如何通过 db:seed 做到这一点?
【问题讨论】:
标签: laravel laravel-5 laravel-5.6