blueprint icon indicating copy to clipboard operation
blueprint copied to clipboard

Ability to add default newFactory() method to newly generated models

Open arnonm opened this issue 3 years ago • 1 comments

Synopsis:

From Laravel documentation: https://laravel.com/docs/9.x/database-testing The HasFactory trait's factory method will use conventions to determine the proper factory for the model the trait is assigned to. Specifically, the method will look for a factory in the Database\Factories namespace that has a class name matching the model name and is suffixed with Factory. If these conventions do not apply to your particular application or factory, you may overwrite the newFactory method on your model to return an instance of the model's corresponding factory directly:

Proposed Syntax:

Add a central flag blueprint.generate_new_factory with a default set to false

Expected Behavior:

when flag is set to false, no change to Model files when flag is set to true, the following is added to Model files: use Illuminate\Database\Eloquent\Factories\Factory; use Database\Factories{{ $class }}Factory; ...

/** * Create a new factory instance for the model. * * @return \Illuminate\Database\Eloquent\Factories\Factory */ protected static function newFactory(): Factory { return {{ $class }}Factory::new(); }

arnonm avatar Mar 23 '22 12:03 arnonm

I'm not sure I understand the motivation behind adding this method when Blueprint would generate conventions models. Said another way, when would Blueprint generate a model factory Laravel could not guess?

jasonmccreary avatar Mar 23 '22 15:03 jasonmccreary