#Seeder - populating with foreign keys

1 messages · Page 1 of 1 (latest)

alpine pollen
#

Hello

New to laravel and the docs don’t really cover - give me examples / return values - so posting here

I have a model application which has a has one relationship with applicant. The applicant has a belongs to relationship with application.

The application table has an applicant_id column and the applicant table has an application_id column

I’m trying to write a seeder which when creating the applicant maintains the relationship. I’ve seen that I should use for() to create the applicant but then I’m not sure what to put in the applicant factory to make the application id

Thanks for any help you can offer

patent crag
#

If you're just writing the factories for test case, the applicant definition() could have something like

$atrributes =[
'Application_id' => Application::factory(),
...rest of the definition
]
alpine pollen
#

Yeah I have that but then the applicant factory makes a distinct application from the one the application has made. I was wondering if they could share the same ID so as to maintain a link

#

The database doesn’t feel clean otherwise which seems to defeat the point of seeding

#

I was expecting something like applicant_id => applicant::factory($this)

timber cape
#

Application::factory()->for(Applicant::factory())->create() should not create an extra Application. If it does, theres something wrong with either you Applicant factory or the application() relationship.