Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 foreign keys in model factory

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?

like image 560
adam78 Avatar asked Sep 06 '15 15:09

adam78


Video Answer


1 Answers

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.

like image 189
Daniele Sassoli Avatar answered Oct 09 '22 04:10

Daniele Sassoli