I have a users
table and a one-to-zero/one relation with a businesses
table (users.user_id => businesses.user_id). On my users
table I have a discriminator which tells me if the user is of type business and therefore I need to have details on the businesses
table as well.
I want to create my Users with my factory which currently is working and then only create business details where the discriminator points to a business account.
I have three options in my mind:
user_id
that the user was assigned. user_id
of the already create user with the business factory user_id
. users.user_id
and business.user_id
. However I am using a random generator for user.user_type
so even if I have the businesses
table filled it might be for users that have the discriminator as 'personal'.Is there another way? Can I pass arguments from my Seeder to the factory?
The attributes you pass to the create
function will be passed into your model definition callback as the second argument.
In your case you don't even need to access those attributes, since they'll automatically be merged in:
$business = factory(App\Business::class)->create();
factory(App\User::class, 5)->create([
'business_id' => $business->id,
]);
Adapt this to your needs.