create factory in laravel code example

Example 1: php artisan make factory

php artisan make:factory AddressFactory --model="App\\Address"

Example 2: laravel factory

php artisan make:factory PostFactory --model=Post

Example 3: make model factory and controller laravel

php artisan make:model ModelName -a
// -a stands for all (Model, Controller, Factory and Migration)
// Note: The above command will work successfully in Laravel 5.5 or > versions

Example 4: add factory data laravel

For Laravel version 7.* and less
factory(App\User::class, 3)->make();

Use create method to persist them to the database:
factory(App\User::class, 3)->create();

Tags:

Php Example