Laravel 5.1 foreign keys in model factory

孤街醉人 提交于 2019-12-10 13:10:00

问题


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?


回答1:


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.




回答2:


The way that the Laravel team, Otwell, Stauffer et.al., suggest is like this.

Testing > Adding Relations To Models

Adding relationships to your models

ModelFactory.php

$factory->define(App\Organisation::class, function ($faker) {
   return [
      'name' => $faker->company,
      'country_id' => 197,
   ];
});

$factory->define(App\Country::class, function ($faker) {
   return [
      'name' => $faker->country,
   ];
});

seeder

$organisations = factory('App\Organisation', 3)
  ->create()
  ->each(function($$organisation) {
      $organisation->relatedItems()->save(factory('App\Country')->make());
  });


来源:https://stackoverflow.com/questions/32425420/laravel-5-1-foreign-keys-in-model-factory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!