How do you define foreign keys in a factory model. For example, if I have an organization table that has a foreign key for the country table, in my factory model I need to define a dummy value for the country identifier as follows:
$factory->define(App\Organisation::class, function ($faker) { return [ 'name' => $faker->company, 'country_id' => 197, ]; });
In my organizational class seeder classes, I do the following, but there is no faker object - do I need to create a new faker object in my seeders class?
use Illuminate\Database\Seeder; class OrganisationsTableSeeder extends Seeder { public function run() { $countryIds = Country::lists('id')->all(); factory('App\Organisation', 3)->create([
Database Seeder Class
class DatabaseSeeder extends Seeder { public function run() { Model::unguard(); $this->call('CountriesTableSeeder'); $this->call('OrganisationsTableSeeder'); Model::reguard(); } }
What is the best way to define foreign keys when defining model factories? Is it possible to omit country_id from the factory model and add it to the seeder class instead - from the document, it looks like you can override the existing value defined in the factory model, but you cannot add a new value through the seeder class - correct me if I'm wrong?
adam78
source share