Reputation: 557
Say that I use a factory to create a model that has 2 properties:
$factory->define(MyModel::class, function (Faker $faker) {
return [
'property1' => $faker->numberBetween(1,10),
'property2' => $faker->numberBetween(1,10)
]
}
If I set the value of property1: factory('App\MyModel')->create(['property1' => 5]), but also want to use that value in order to calculate the value of property2 (for example have property2 equal property1 + 10), how do I access it inside the factory?
Upvotes: 0
Views: 450
Reputation: 3185
Try something like this to access the previous property,
$factory->define(MyModel::class, function (Faker $faker) {
$property1 = $faker->numberBetween(1,10);
return [
'property1' => $property1,
'property2' => $faker->numberBetween(1,10) + $property1
]
}
Upvotes: 2