我有一个工厂,它被调用了几次,但是在contract_year
列上我得到了相同的值。
$factory->define(ContractYear::class, function (Faker $faker) {
$contract = Contract::orderByDesc('id')->first();
$contract_year = ContractYear::select('contract_year')->orderByDesc('id')->value('contract_year');
if($contract_year == null){
$contract_year = 2019;
}
return [
'contract_id' => $contract->id,
'contract_year' => $contract_year++,
'licensed_users' => $faker->randomDigit,
];
});
我从这里开始..
$u->contracts()->saveMany(factory(Contract::class, rand(1, 5))->create()->each(function ($contract){
$contract->years()->saveMany(factory(ContractYear::class, $contract->number_of_years)->create());
}));
2条答案
按热度按时间wztqucjr1#
您可以使用
static
参考:
static
keyword inside function?3wabscal2#
将计数器附加到
$this
可能值得研究一下Laravel 8新模型工厂,以获得更明确的面向对象的方法