factory relationship laravel 8 code example

Example 1: laravel factory relations data

factory(App\User::class, 30)->create()->each(function($user) {

    $entity = factory(App\Entity::class)->make();

    $address = factory(App\Address::class)->create([
        'entity_id' => $entity
    ]);

    $user->entities()->save($entity);
});

Example 2: 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