I'm refactoring laravel factories. Then I found a define function like below.
$factory->define(EmploymentAllowance::class, function (Faker $faker, array $attributes) {
$employment = Arr::exists($attributes, 'employment_id')
? Employment::where('id', $attributes['employment_id'])->first()
: Employment::factory()->create();
return [
'employment_id' => $employment->id,
'calendar_id' => fn () => Calendar::factory()->create([
'company_id' => $employment->company_id,
]),
];
});
I have refactored this like below.
public function definition()
{
$employment = fn (array $attributes) => Arr::exists($attributes, 'employment_id')
? Employment::where('id', $attributes['employment_id'])->first()
: Employment::factory()->create();
return [
'employment_id' => $employment->id,
'calendar_id' => fn () => Calendar::factory()->create([
'company_id' => $employment->company_id,
]),
];
}
Now I'm getting an error like this,
Error: Closure object cannot have properties
I have been trying to fix this for a long time now. How can I fix this ??