Answers for "laravel factory make relationship"

PHP
1

laravel factory relations data

factory(AppUser::class, 30)->create()->each(function($user) {

    $entity = factory(AppEntity::class)->make();

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

    $user->entities()->save($entity);
});
Posted by: Guest on December-19-2020
0

laravel factory relationship one to many

// parent factory 

    public function configure()
    {
        return $this->afterCreating(function (Category $category) {
            $posts = Post::factory(50)->make();
            $category->posts()->saveMany($posts);
        });
    }

// call in seeder
Category::factory(10)->create();
Posted by: Guest on January-04-2022

Code answers related to "laravel factory make relationship"

Browse Popular Code Answers by Language