Solution:
Probably not what you’re looking for but here goes:
You can create an inline class that defines a function that returns the real function you want to use. That inline class can use a trait.
Note: Anonymous classes were added in PHP 7
<?php
use App\Working;
use App\Traits\Calculate;
// ...
$factory->define(App\Working::class, (new class {
use Calculate;
public function generatorFunction() {
return function (Faker\Generator $faker) {
// ...
$getData = $this->getData();
// ...
return ['get_data' => $getData];
};
}
})->generatorFunction());
Update:
Since Laravel 8 model factories are now classes instead of functions so can use traits making the above solution unnecessary.