Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run specific factory in laravel 8

I need to run factory using tinker but my model isn't in app folder like this->App\Models\User::factory()->count(2)->create();

the path is-> Modules\Menu\Entities\MenuPosition and when I run tis command

php artisan tinker
Modules\Menu\Entities\MenuPosition::factory()->count(2)->create();

there is error

Class 'Database/Factories/Modules/Menu/Entities/MenuPositionFactory' not found in C:/laragon/www/menu--test/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php on line 656
like image 430
arefeh Avatar asked Oct 23 '25 14:10

arefeh


2 Answers

you can use like this:

\App\Models\User::factory()->count(2)->create();
like image 57
Amir Reza Tavakolian Avatar answered Oct 25 '25 03:10

Amir Reza Tavakolian


You can override the newFactory method on this model:

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

Or you can look into the documentation to see how to adjust how the factory is looked up based on the model.

like image 44
lagbox Avatar answered Oct 25 '25 03:10

lagbox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!