laravel test post object in db code example

Example 1: test laravel create each(function ($u)

public function run()
{
  factory(App\User::class, 2)->create()->each(function($u) {
    $u->issues()->save(factory(App\Issues::class)->make());
  });
}

Example 2: 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);
});

Tags:

Php Example