$factory->define(App\Client::class, function (Faker\Generator $faker) {
static $password;
$company_name = $faker->Company;
return [
'name' => $company_name,
'short_name' => substr($company_name, 0, 3),
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});
$factory->define(App\Campaign::class, function (Faker\Generator $faker) {
static $password;
return [
'unique_id' => $faker->numerify('ABC###'),
'client' => function() {
return factory('App\Client')->create()->id;
}
];
});
I am generating come clients and campaigns. One client can have many campaigns
How do I take the short_name from the Company and pass it to a the campaign class so I can append it to a random string to create a unique id in the client?
You're almost there. You don't need to use an anonymous function in the campaign class, you can just reference the factory
directly. Use a variable inside the Campaign factory and just reference whatever values you need.
$factory->define(App\Campaign::class, function (Faker\Generator $faker) {
$client = factory(App\Client::class)->create();
return [
'unique_id' => $faker->numerify('ABC###') . $client->short_name,
'client' => $client->id
];
});