Level 27
Define $model in the factory
I have a package with models
\My\Package\Models\Buyer
\My\Package\Models\Seller
\My\Package\Models\Product
So naturally, factories would be
\Database\Factories\My\Package\Models\BuyerFactory
\Database\Factories\My\Package\Models\SellerFactory
\Database\Factories\My\Package\Models\ProductFactory
Seller::factory(10)
->has(Product::factory()
->hasDetails([inventory => 1, price => 10])
->hasDetails([inventory => 1, price => 10])
->hasDetails([inventory => 1, price => 10])
)
->create();
But I get the Error: Class "App\Product" not found
What should be the convention to write factory test code in a package just like the way in a Laravel project?
Also can see https://laracasts.com/discuss/channels/laravel/seeding-nested-relationship-with-factory
Laravel attempts a guess at the namespace based on its conventions; you are not working within the conventional namespace(s) so you must define how a new factory is instantiated.
https://laravel.com/docs/11.x/eloquent-factories#factory-and-model-discovery-conventions
// Product model
protected static function newFactory()
{
return \Database\Factories\My\Package\Models\ProductFactory::new();
}
Please or to participate in this conversation.