Laravel 5.1模型工厂的外键 [英] Laravel 5.1 foreign keys in model factory
问题描述
如何在模型工厂中定义外键?例如,如果我有一个组织表有一个外键的国家表,在我的模型工厂,我不得不为国家ID定义一个虚拟值,如下所示:
$ factory-> define(App \Organisation :: class,function($ faker){
return [
'name'=> $ faker->公司,
'country_id'=> 197,
];
});
在我的组织表播种器类中,我做了以下工作,但是faker对象不存在 - do我需要在我的播种班上创建一个新的faker对象?
使用Illuminate \Database\Seeder;
class OrganisationsTableSeeder extends Seeder
{
public function run()
{
$ countryIds = Country :: lists('id' ) - >所有();
$ b $ factory('App \ Organization',3) - > create([
// //覆盖出厂默认值
'country_id'=> $ faker-> randomElement [$ countryIds],
]);
$ p $数据库播种类
class DatabaseSeeder extends Seeder
{
public function run()
{
Model :: unguard();
$ this-> call('CountriesTableSeeder');
$ this-> call('OrganisationsTableSeeder');
Model :: reguard();
$ / code $ / pre
定义外键的最佳方式是什么模型工厂?是否有可能从模型工厂中省略country_id并将其添加到seeder类中 - 从文档看来,您只能覆盖在模型工厂中定义的现有值,但不能通过播种器类添加新值 - 正确我如果我错了?
解决方案我可能有点晚,但我有同样的问题,这给我修好了你应该可以这样做:
$ $ $ $ $ $ $ $ $ $ $ $ factory $> define(App_Organisation :: class,function($ faker) {
return [
'name'=> $ faker-> company,
'country_id'=> factory(App\Country :: class) - > create() - > id,
];
});
然后在你的种子里你只需要打电话给
$ b $ (> create();
它也会为您创建国家。
How do you define foreign keys in a model factory. For example if I have a organisations table which has a foreign key to the countries table, in my model factory I'm having to define a dummy value for the country id as follows:
$factory->define(App\Organisation::class, function ($faker) {
return [
'name' => $faker->company,
'country_id' => 197,
];
});
In my organisations table seeder class I am doing the following but the faker object isn't present - do I need to create a new faker object in my seeder class?
use Illuminate\Database\Seeder;
class OrganisationsTableSeeder extends Seeder
{
public function run()
{
$countryIds = Country::lists('id')->all();
factory('App\Organisation', 3)->create([
// override factory default
'country_id' => $faker->randomElement[$countryIds],
]);
}
}
Database seeder class
class DatabaseSeeder extends Seeder
{
public function run()
{
Model::unguard();
$this->call('CountriesTableSeeder');
$this->call('OrganisationsTableSeeder');
Model::reguard();
}
}
Whats the best way to define the foreign keys when defining model factories? Is it possible to omit the country_id from the model factory and add it in the seeder class instead - from the documention it seems you can only override an existing value defined in the model factory but you cant add a new value via the seeder class - correct me if i'm wrong?
I may be a bit late on this one but I was having the same issue, this fixed it for me. You should be able to do
$factory->define(App\Organisation::class, function ($faker) {
return [
'name' => $faker->company,
'country_id' => factory(App\Country::class)->create()->id,
];
});
and then in your seed you just need to call
factory(App\Organisation::class, 5)->create();
and it will create the countries for you as well.
这篇关于Laravel 5.1模型工厂的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!