php - Laravel 5.1 Seeding with Factories -
i'm trying out new laravel 5.1 model factories seed app's tables.
using info on http://laravel.com/docs/5.1/seeding#using-model-factories built this:
// account model class account extends model { public function contacts() { return $this->hasmany('app\contact'); } // contact model class contact extends model { public function account() { return $this->belongsto('app\account'); } // account table seeder $accounts = factory(app\account::class(), 25)->create()->each(function($u) { $u->contacts()->save(factory(app\contact::class)->make());
the problem foreign key never gets set (the corresponding account_id
should passed in contact
table isn't being passed).
i tried setting account_id
manually this:
$u->contacts()->save(factory(app\contact::class)->make([ 'account_id' => $u->id, ]);
but fails, , anyway it's not mentioned needed in docs.
has been using successfully?
apparently since used non-standard primary key names, have supply both local , foreign key columns.
since i'd specified primary key in migration thought laravel pick on that.
so make work have following in model:
// account model class account extends model { public function contacts() { return $this->hasmany('app\contact', 'account_id', 'account_id'); }
once did seeding worked expected.
Comments
Post a Comment